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:
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.
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:
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.
gls = car("Audi","Q7","SUV",4)
q7.car_type
q7.car_details()
Creating a class for Ford Mustang and accessing its member functions and variables.
mustang = car("Ford","Mustang","Sedan",2)
mustang.brand
mustang.car_details()
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.
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.
gls = car("Mercedes","GLS","SUV",5)
gls.seating_capacity
gls.car_details()
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.
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)
model_3 = car()
Calling car_attributes to initialize attributes.
model_3.car_attributes("Tesla", "Model 3", "Sedan", "5")
model_3.brand
model_3.car_details()