Python Object Oriented Programming


Understanding Object Oriented Programming

Python is a multi-paradigm programming language. Python supports different programming approaches. One of the most popular approaches to solve programming problem is by creating class and objects. This approach is known as Object-Oriented Programming (OOP).The core concept of Object oriented Programming is to create reusable code. This is also known as DRY (Don’t repeat Yourself). Let us take an example to showcase OOP’s concept.

A person can be an object, as it has the following properties:

  • Name, age as attributes
  • Studying, working as behavior.

Some basic principles of OOP in python:

Object is an instance of a class.

A class is a blueprint for the object. We can consider a person with labels containing the details about the name, age, height, weight etc.  These descriptions or attributes of the Person class is used to define the class.

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

Major aspects of object-oriented programming


The major feature of Encapsulation is as follows:

  1. An object’s behavior is kept hidden from outside world or objects keep their state information private
  2. Clients can’t change the objects internal state by directly acting on them; rather, clients request the object by sending messages. Based on the type of the requests, objects may respond by changing their internal state using special member functions such as get and
  3. In python, the concept of encapsulation (data and method hiding) is not implicit, as it doesn’t have keywords such as public, private, and protected (in languages such as C++ or Java) they are required to support encapsulation. Of course, accessibility can be made private by prefixing “__” in the variable or function name.

Code Description :

In the example, a class Chocolate is defined with an object of selling Price i.e sell. c.sell() gives the first output having value 290.Then the price is tried to modify by providing a value of 1000 to the attribute. However, the value can’t be changed because Python treats the __maxprice as private attribute. Now a setter function is called i.e setMaxPrice() which can take price as a parameter.


The major feature of polymorphism is as follows:

  1. An object provides different implementations of the method based on input parameters
  2. The same interface can be used by objects of different types.

Code Description :

In the example, two classes Sparrow and Penguin is created. Both the classes have object describing their flying and swimming capabilities. However, their functions are different. A common interface is defined showing the flight capability i.e flying_capablility( ). Two objects Sp and Pe are passed in flying_capablity() function. The output is  rendered for only the flight property.


The major feature of inheritance is as follows:

  1. Inheritance indicates that one class derives(most of its) functionality from the parent class.
  2. Inheritance is described as an option to reuse functionality defined in the base class and allow independent extensions of the original software implementation.
  3. Inheritance creates hierarchy via the relationships among objects of different classes. Python, unlike Java supports multiple inheritance (inheriting from multiple base classes).

Code Description :

In the example, a parent class B is made, which consists of two child class. The child class B takes in a parameter of child class A and defines an object b. Here class B inherits the print statement of class A  and results the output of class A through class B’s object.  


The major feature of abstraction is as follows:

  1. It provides you with a simple interface to the clients, where the clients can interact with class objects and call methods defined in the interface
  2. It abstracts the complexity of internal classes with an interface so that the client need not be aware of internal implementations

Code Description :

In the example internal details of the Adder class are abstracted with the add() method. A for loop with range upto 99 is created and the add object is called passing the variable “i”. 


The major feature of composition is as follows:

  1. It is a way to combine objects or classes into more complex data structures or software implementations.
  2. In composition, an object is used to call member functions in other modules thereby making base functionality available across modules without inheritance.

Code Description :

 In this example, class B having object b is calling another classes object . The base functionality is available to the module without inheritance. 

The 5 solid Object-oriented design principles

Object oriented design principles help us think about ways that increases the system’s maintainability and extensibility. The object oriented design principle acts as a toolbox for design patterns. 


Single Responsibility




Liskov substitution


Interface Segregation


Dependancy inversion

The single responsibility principle states, a class should only have one reason to change. 

The open/close principle states that classes or objects and methods should be open for extension but closed for modification

The substitution principle states that, derived classes must be able to completely substitute the base class.

The interface segregation principle states, clients should not be forced to depend on interfaces they don’t use. 

The inversion of control principle states that high-level modules shouldn’t be dependent on low-levek modules; they should be dependent on abstractions. Details should depend on abstractions and not the way around.

We will get started to design patterns in python in the next Python series.



Leave a Reply

Your email address will not be published.