Python Object Oriented Programming

Python supports functional programming as well as Object Oriented Programming. So Python language is multi-paradigm programming language.

When we talk about Object Oriented Programming paradigm, attribute and behaviour are the most common character rustic in object we consider. So lets look at how do we create Object in Python.

Class

A class is a blueprint for the object.

We can think of class as a sketch of a Human with labels. It contains all the details about the name, age, height etc. Based on these descriptions, we can study about the Human. Here, a Human is an object.

class Human:
    pass

Here, we use the class keyword to define an empty class Human. From class, we construct instances. An instance is a specific object created from a particular class.

Object

An object is an instantiation of a class. When class is defined, only the description for the object is defined. Therefore, no memory or storage is allocated.

obj = Human()

Here, obj is an object of class Human.

Methods

Methods are functions defined inside the body of a class. They are used to define the behaviors of an object.

class Human:
    
    # instance attributes
    def __init__(self, first_name, last_name, age):
        self.first_name = first_name
        self.last_name = last_name
        self.age = age
    
    # instance method
    def full_name(self):
        return "{} {}".format(self. first_name, self.last_name)


# instantiate the object
same = Human("Same", "Peter", 30)

# call our instance methods
print(same.full_name())

Output

Same Peter

Inheritance

Inheritance is creating a new class of using detail from existing class. Newly formed class we call derived class (Child class) and class we used for that we call it base class( parent class). In Python, Inheritance works differently.

# base class
class Human:
    
    def __init__(self):
        print("Human is created")

    def whoisThis(self):
        print("Human")

# child class
class Man(Human):

    def __init__(self):
        # call super() function
        super().__init__()
        print("Man is created")

    def whoisThis(self):
        print("Man")


human = Man()
peggy.whoisThis()

Output

Human is created
Man is created
Man

Encapsulation

we can restrict access to methods and variables. This prevents data from direct modification which is called encapsulation.

In Python,
we denote private attributes using double underscore as the beginning of the variable name i.e double __ and
protected attributes using single underscore as the beginning of the variable name i.e single _.

Protected member

# base class
class Human:
    __baseType = "Human"
    def __init__(self):
        print("Human is created")

    def whoisThis(self):
        print("Human")

# child class
class Man(Human):

    def __init__(self):
        # call super() function
        super().__init__()
        print("Man is created")

    def whoisThis(self):
        print("Man")
        print("Base Type is ", self.__baseType)

Output

Human is created
Man is created
Man
Base Type is Human

Private member

# base class
class Human:
    _type = "Human"
    def __init__(self):
        print("Human is created")

    def whoisThis(self):
        print("Type is ", self._type)

Output

Human is created
Type is Human

Polymorphism

Polymorphism is an ability to use a common interface for multiple forms.

Suppose, We have Man and Woman classes, but both classes, we could use same common methods and attributes when considering objects. So we call it Polymorphism.

class Man():

    def whoisThis(self):
        print("Man")

class Woman():

    def whoisThis(self):
        print("Woman")

def Human_Test(human):
    human.whoisThis()

#instantiate objects
man = Man()
woman = Woman()

# passing the object
Human_Test(man)
Human_Test(woman)

To use polymorphism, we created a common interface i.e Human_Test() function. So Human_Test function will call whoisThis() method in every object pass in.

Output

Man
Woman

Leave a Reply