Classes are templates used to define the properties and methods of objects in code. They can describe the kinds of information that the class holds, and also how a programmer interacts with that data.
It serves as a blueprint or template for the objects, or “instances” of the class.
Creating a Class
In Python, classes are defined using the
Class definitions cannot be empty, so the
pass statement can be used as a placeholder to avoid errors.
Creating Instances of a Class
Objects can be created from classes. These objects are called instances of a class, and when we create an instance, that is known as instantiating a class.
To create an instance of a class, we can set a variable equal to the class name followed by parentheses
Here, the instance name is
my_home, and the class is
Methods are functions defined as part of a class. The first parameter for any class method is the actual object calling the method, usually called
For example, the following class
Home has a method called
- The first parameter is
- The second parameter is
When calling a class method, no argument is provided for the parameter
self, but arguments must be provided for each following parameter.
Class attributes are variables that are defined outside of all methods and have the same value for every instance of the class.
Instance variables are variables that are unique to each instance of a class. They can be set initially within the
__init__() method when the instance is created, or instance variables can be added by setting them to values.
We give the instance
dog a new instance variable called
name and set it to the value
This method is used to initialize a newly created object. It is called each time a class is instantiated. Instance variables are set within the
__init__() method block. Input parameters can be set for this method, which are passed during instantiation.
__repr__() method returns the string representation of the class. One way to see the string representation is to call
print() on the instance.
Accessing Methods and Variables in the Class
Within the code of the class, we can call its methods and access its variables. We do this by using
self followed by a period
. and then followed by the method or variable.
type() function returns the data type of the argument passed to it. When the argument is an instance of a class, it returns the class that it is an instance of.
hasattr() function can be used to check if an instance of a class has an attribute. It returns
True if it does have the attribute, and
Objects, or instances of classes, can be deleted using the
Attributes of objects can be deleted using the
Lets demonstrate a class for an
Once we create an
Employee class like we did above we can import the module in our code and create instances of it whenever and wherever we want, thereby enabling code reusablity and making our code more efficient and easier to maintain.
When creating applications classes are a great way to organize and improve the quality of your code.