Python Class and Object

Object oriented programming(OOP) is a major concept in programming. Python support object oriented programming. Objects are instances of class which has states and behavior. A class can be defined as a blueprint or template which defines state and behavior for an object to be created. In OOP, you can represent real world entities with classes and objects.

Defining a class 

 A class is defined using class keyword. It has variables and methods inside it that represents real world entities. For instance, 

class Car:
    engine = "petrol engine"
    color = "blue"

    def display_info(self):
        print("Engine: ", Car.engine, "Color: ", Car.color)

Here Car is a class. It has engine and color as it’s attributes(variables), and it has display_info() as it method.

Creating an object

Object can be creating by calling the class similar to calling a function. The variable will be an instance and the instance can access the attributes and method inside it. Let’s see an example

class Car:
    engine = "petrol engine"
    color = "blue"

    def display_info(self):
        print("Engine: ", Car.engine, "Color: ", Car.color)

obj = Car()
obj.display_info()

Here, obj is an object which can access attribute(such as engine, and color) and method(such as display_info()) inside class .

 Constructor in a class

Constructor are special method which is executed every time an object of a particular class is created. In python it is represented using __init__() special method. Constructor can also take arguments passed to a class. Let’s see some example,

constructor without parameter

class Car:
    # This is a constructor in python
    def __init__(self):
        print("constructor called")
        self.engine = "petrol engine"
        self.color = "blue"

    def display_info(self):
        print("Engine: ", self.engine, "Color: ", self.color)


obj = Car()
# let's also print engine and call display_info() method
print(obj.engine)
obj.display_info()

Output:

constructor called
petrol engine
Engine:  petrol engine Color:  blue

Here __init__(self) represents a constructor. The self is a python signature for passing object. You can give any name in place of same like this, let, or any other valid variable name. For instance this code perform same above operation using this in place of self 

class Car:
    # This is a constructor in python
    def __init__(this):
        print("constructor called")
        this.engine = "petrol engine"
        this.color = "blue"

    def display_info(this):
        print("Engine: ", this.engine, "Color: ", this.color)


obj = Car()
# let's also print engine and call display_info() method
print(obj.engine)
obj.display_info()

Also a point to note is despite we have defined self as parameter inside display_info(self) method we do not have to  pass self while calling this method using the object as seen in obj.display_info().

Constructor with parameter:

class Jeep:
    # This is a constructor in python
    def __init__(self, engine, model, color):
        print("constructor called")
        self.engine = engine
        self.model = model
        self.color = color

    def display_info(self):
        print(self.engine,  self.model, self.color)


obj = Jeep('Petrol engine', 'BMW', 'Blue')
# let's also print engine and call display_info() method
print(obj.engine)
obj.display_info()

Output:

constructor called
Petrol engine
Petrol engine BMW Blue

Here engine, model and color are passed as parameter. These parameter should be supplied with values while creating the object as seen in Jeep(‘Petrol engine’, ‘BMW’, ‘Blue’) statement. If you don’t have parameter you don’t have to supply them while creating object, otherwise you have to supply them when creating an object.

Destructor in python:

Like constructor a destructor is  a special method which is called every time an object of a particular class is deleted. This special method is created using __del__(self) . It does not take any parameter. Deletion of an object is done using del keyword. Let’s see an example:

class Bus:
    def __init__(self):
        print("constructor method")

    def __del__(self):
        print("destructor method")


bus_object = Bus()
del bus_object

Output:

constructor method
destructor method

Leave a Reply

Your email address will not be published. Required fields are marked *