Classes and objects are backbones of object-oriented programming in any programming language. In object-oriented programming, any entity that is capable of performing any function and has some attributes is implemented as a class. In this article, you will study what classes and objects are, the differences between the two, and how to implement them in Python.
A class serves as a blueprint for creating objects. A class is similar to the map of a house. With the help of a map, you can find the number of bedrooms, bathrooms, dining rooms, and other parts of a house. The same is the case with a class. With class, you define the types of attributes and methods that the object of a class will contain. You can build multiple houses using one map. In the same way, you can create multiple objects using one class.
Syntax of a Python Class
class ClassName: '''This is the body of the classs''' pass
Let’s now create a meaningful class. Suppose, you are developing a racing car game and you have to develop a class for the entity
Vehicle. You can do so as follows:
class Vehicle: ## Instance Methods def show_details(self): self.vehicle_name = "Honda" self.vehicle_price = 10000 print(self.vehicle_name) print(self.vehicle_price)
The script above creates a class named
Vehicle class contains one member method
show_details() and two member variables:
vehicle_price. It is pertinent to mention that the instance variables are initialized inside an instance method and the first parameter to the instance method by default is
show_details() method is an instance method that shows the values of both the member variables.
Objects are instances of a class, hence, the process of creating objects is called instantiation. Let’s create a simple object of the Vehicle class.
my_car = Vehicle() my_car.show_details()
In the script above, we create an object of the Vehicle class called
my_car. To access the object’s method, you have to write the name of the object followed by a dot operator and the name of the function. In the script above we call the
my_car.show_details() function to display the vehicle’s details. Here is the output:
Similarly, you can access the object’s member like this:
A constructor is a special method that is called whenever you create an object of a class. You need to define
__init__() method in order to create a class constructor. The following script adds a constructor to our Vehicle class. The variables
vehicle_price are initialized within the constructor.
class Vehicle: def __init__(self, name, price): self.vehicle_name = name self.vehicle_price = price def show_details(self): print(self.vehicle_name) print(self.vehicle_price)
In the above script, the constructor has three parameters:
price. The first parameter i.e.
self refers to the object that is being instantiated, you do not have to pass the value for the parameter
self. However, while creating the object of the
Vehicle class, you have to pass the values for the
price parameters . These parameter values initialize the
vehicle_price variables respectively.
Let’s create two objects of the
Vehicle class using the class constructor:
my_car2 = Vehicle("Ford", 25000) my_car2.show_details() my_car3 = Vehicle("Toyota", 10000) my_car3.show_details()
Ford 25000 Toyota 10000
Instance Members vs. Class Members
A class can have two types of members (variables and methods): instance members and class members. Instance members as the name suggests are those variables or methods that belong to an individual object and are not shared among objects of the same class. Class members, on the other hand, consist of variables and methods that are shared by all the instances of a class. The following example explains this concept.
class Vehicle: ## class variable count = 0 ## constructor def __init__(self, name, price): self.vehicle_name = name self.vehicle_price = price Vehicle.count += 1 ## instance method def show_details(self): print(self.vehicle_name) print(self.vehicle_price) ## class method def show_vehicle_count(): print(Vehicle.count)
In the above script, we define a
Vehicle class that contains one class variable
count and two instance variables
vehicle price. As seen in the script above, to create instance variables, you need to prefix the keyword
self before the variable names. Also, you have to initialize instance variables inside an instance method. On the other hand, class variables are defined outside any method or constructor and you can access them via the class name.
Vehicle class also contains an instance method
show_details() and a class method
show_vehicle_count(). The difference between a class method and instance method is the
self parameter which is the first parameter in the case of an instance method. On the contrary, a class method doesn’t contain any
Vehicle class, the class constructor initializes the
vehicle_price variables, while it increments the class variable
count. Let’s create objects of the
Vehicle class and see the difference between class and instance variables:
my_car4 = Vehicle("BWM", 300000) my_car4.show_details() ## calling instance method Vehicle.show_vehicle_count() ## calling class method
In the script above, we create an object of the Vehicle class and then call the
show_vehicle_count methods. You can see that we call the instance method
show_details() via the object name and the class method
show_vehicle_count() via the class name. Since the initial value of the count variable is 0, creating an object of the Vehicle class will increment it to 1. Here is the output:
BWM 300000 1
Now when you create another object of the Vehicle class, the value of the count variable will be incremented to 2 since all the instances share this variable. Therefore, you will see 2 in the output as shown below:
my_car5 = Vehicle("Renault", 26000) my_car5.show_details() ## calling instance method Vehicle.show_vehicle_count() ## calling class method
Renault 26000 2