Object-Oriented Programming in Python

Object-oriented programming (OOP) refers to the software design wherein programmers define the data type of a data structure, and the types of functions that can be applied to the data structure. This paradigm provides functionalities and behavior pattern to the structure of data.

This paradigm maps and models real-world things together and describes a relationship among them. OOP models real-world entities as software objects, which have data associated with them and have some behavioral patterns (functions).

Python Oop
Object-Oriented Programming Paradigm

Classes in Python

Class is a blueprint of the functionality of an entity. Classes are used to create new user-defined data structures that contain arbitrary data. In the case of an animal, we could create an Animal class to track properties about the Animal, like the name and age.

Consider class as a blueprint of an Animal with labels. It contains all the details about the name, size, etc. Based on these descriptions, we can study about the Animal. Here, Animal is an object.

Syntax:

class class_name:
  pass

Example:

class Animal:
  pass

Objects in Python

An object is an instance of a class. When class is defined, only the description for the object is defined. Hence, no memory is allocated. An Object represents the class along with its functionality and behavior.

Syntax:

object_name = Class_name(argument list)

Example:

class Dog:

    # class attribute
    species = "animal"

    # instance attribute
    def __init__(self, name, age):
        self.name = name
        self.age = age

# instantiate the Dog class i.e create objects
A = Dog("Blu", 10)
B = Dog("Woo", 15)

# access the class attributes
print("A is a {}".format(A.__class__.species))
print("B is also a {}".format(B.__class__.species))

# access the instance attributes
print("{} is {} years old".format( A.name, A.age))
print("{} is {} years old".format( B.name, B.age))

Output:

A is a animal
B is also a animal
A is 10 years old
B is 15 years old

Data Abstraction in Python

Abstraction is used to hide internal details and display the necessary functionalities. Abstraction means displaying the content in such a manner that only the essential functions get displayed to the user according to the privileges and the rest of the internal working stays hidden.


Encapsulation in Python

Encapsulation refers to the binding of data and functions into a single unit. A class represents encapsulation as it binds the functionality and behavior into a single unit and represents it as Objects.


Inheritance in Python

In the world of Object-Oriented Programming (OOP), Inheritance refers to the mechanism of the capability of a class to derive or extend the properties from another class in the run. This property enables the derived class to acquire the properties or traits of the base class.

Inheritance is considered one of the most important aspects of OOP because it serves the feature of re-usability, thus making the piece of code more reliable.

Oops Inheritance
Inheritance

Example:

# Base class
class Dog:

    # Class attribute
    species = 'mammal'

    # Instance attributes
    def __init__(self, name, age):
        self.name = name
        self.age = age

    # instance method
    def description(self):
        return "{} is {} years old".format(self.name, self.age)

    # instance method
    def speak(self, sound):
        return "{} says {}".format(self.name, sound)


# Derived class (inherits from Dog class)
class Bulldog(Dog):
    def run(self, speed):
        return "{} runs {}".format(self.name, speed)


# Derived class inherits attributes and
# behavior from the parent class
Jim = Bulldog("Jim", 12)
print(Jim.description())

# Derived class has specific attributes
# and behavior as well
print(Jim.run("slowly"))

Output:

Jim is 12 years old
Jim runs slowly

Polymorphism in Python

This phenomenon refers to the ability to be able to display in multiple forms.

Suppose, we need to color a shape. There are multiple shape options (rectangle, square, circle). However, we could use the same method to color any shape. This concept is called Polymorphism.

Oops Polymorphism
Polymorphism

Example:

class Rectangle:

    def draw(self):
        print("Drawing a rectangle.")
    
    
class Triangle:

    def draw(self):
        print("Drawing a Triangle.")
    
    

# common interface
def draw_shape(Shape):
    Shape.draw()

#instantiate objects
A = Rectangle()
B = Triangle()

# passing the object
draw_shape(A)
draw_shape(B)

Output:

Drawing a Rectangle.
Drawing a Triangle.

References: