Python Class


Class is famously said to be the blueprint of the object. We can say the class is a collection of data members and member functions. By data members we mean to say the variables that objects of the class can access and by member functions we mean to say the methods that objects of the class can access.

Classes are made using the class keyword followed by the name of the class.

Example:

In [1]:
class employee:
    pass

Below is the example of the car class with members as brand, model, car_type, seating_capacity and with member function as car_details to return the detail of the car.

In [1]:
class car:
    def __init__(self, brand, model, car_type, seating_capacity):
        self.brand = brand
        self.model = model
        self.car_type = car_type
        self.seating_capacity = seating_capacity
    
    def car_details(self):
        print(self.brand +" "+ self.model + " is a " + self.car_type)

Objects

We can say that object is the instance of the class. Here in this example we will create object of class ‘car’ for Audi Q7. While creating, we will initialize all the class variables. This object will be able to access all the class variables and methods. Then we will create new object for Ford Mustang with it’s own separate attributes.

Example:

In [1]:
class car:
    def __init__(self, brand, model, car_type, seating_capacity):
        self.brand = brand
        self.model = model
        self.car_type = car_type
        self.seating_capacity = seating_capacity
    
    def car_details(self):
        print(self.brand +" "+ self.model + " is a " + self.car_type)

Creating a class for Audi Q7 and accessing its member functions and variables.

In [2]:
gls = car("Audi","Q7","SUV",4)
In [3]:
q7.car_type
Out[3]:
'SUV'
In [4]:
q7.car_details()
Audi Q7 is a SUV

Creating a class for Ford Mustang and accessing its member functions and variables.

In [5]:
mustang = car("Ford","Mustang","Sedan",2)
In [6]:
mustang.brand
Out[6]:
'Ford'
In [7]:
mustang.car_details()
Ford Mustang is a Sedan

The self Parameter

The self parameter is the mandatory parameter to be passed to every method in the class. It is used to access the class variables in the class methods. It acts as the reference to the instance of the class. It is always supposed to be the first parameter of the class method. Though at most of the places we can see that we use ‘self’ keyword, but self is not a keyword, we can use any name instead of self, it can be your’s name too, but yes, we always prefer to use some meaningful name.

Example:

Using “logical” instead of “self” and it is still working.

In [1]:
class car:
    def __init__(logical, brand, model, car_type, seating_capacity):
        logical.brand = brand
        logical.model = model
        logical.car_type = car_type
        logical.seating_capacity = seating_capacity
    
    def car_details(logical):
        print(logical.brand +" "+ logical.model + " is a " + logical.car_type)

Creating a class for Mercedes GLS and accessing its member functions and variables.

In [3]:
gls = car("Mercedes","GLS","SUV",5)
In [5]:
gls.seating_capacity
Out[5]:
5
In [6]:
gls.car_details()
Mercedes GLS is a SUV

The __init__() Method

The __init__() method is the in-built method that is called whenever we create the object of the class. It is used to assign values to the data members along with doing the other necessary tasks need to be done during the creation of the object. In the __init__() method also, it is mandatory to use the first argument as the self keyword.

Example:

Creating a class and initializing attributes without the use of init method. We are using car_attributes method to initialize attributes.

In [1]:
class car:
    def car_attributes(self, brand, model, car_type, seating_capacity):
        self.brand = brand
        self.model = model
        self.car_type = car_type
        self.seating_capacity = seating_capacity
    
    def car_details(self):
        print(self.brand +" "+ self.model + " is a " + self.car_type)
In [2]:
model_3 = car()

Calling car_attributes to initialize attributes.

In [3]:
model_3.car_attributes("Tesla", "Model 3", "Sedan", "5")
In [4]:
model_3.brand
Out[4]:
'Tesla'
In [5]:
model_3.car_details()
Tesla Model 3 is a Sedan