OOP Concepts in Python With Examples

OOP Concepts in Python


As was mentioned in the previous tutorial an object is an actual physical thing, while the term “class” is a logical definition. A physical object could be anything from an individual student who is creating a record registry for a school or a pen that is in stationary’s item management software or a car within the manufacturing’s car database software. There are three main features that help it be recognized, and they are:

  1. Identity is any piece of information that could use to identify an object of the class. It could include the names of students, or the name of the vehicle and so on.
  2. Properties The properties of the object are referred to as properties. For example, gender, age or DOB for students; or the kind of engine, or number of gears in the car.
  3. Behaviour of an object is in accordance with the functions it is able to accomplish. With OOP there is the possibility of assigning certain tasks to objects belonging to an entire class. Moving forward as a student could write/read, the car could accelerate, and the list goes on.


The term “class” refers to a blueprint in which characteristics and behaviour are defined. For instance, If Mahatma Gandhi Sachin Tendulkar or you me are all objects, that is, then a human Being is an object. The term “object” is the primary idea behind OOP however, classes offer the ability to define similar types of objects.

In class, both the data as well as the functions running on the data are integrated into one unit.

As an example, let’s say there’s a class called Car that contains fundamental information like name model, model, brand dates of production, the engine, etc as well as some functions such as turning on the engine and off, use brakes to accelerate the engine, switch gears, blow horns etc. Once all the fundamental functions are specified in the Car class Car it is possible to build objects using the values for the properties name or model and the class object Car can utilize the functions that are defined within the class.

Data Hiding

Data hiding allows us to determine the data’s privacy that is accessible to the outside world, or, more precisely in other classes. The purpose behind this is to establish different levels of access to the object’s information and protect the accidental alteration of data. Additionally concealing or setting privacy levels, is possible to perform functions, too.

In OOP the data within the classes can be classified in terms of private, publicprivate or secured. Private data or functions are those that can’t be accessed or viewed outside the class. In contrast, public data or functions may be accessed from any location. Functions or data that are protected are more or less public, however, they shouldn’t be accessed by anyone outside.

Abstraction of Data

Classes are based on the idea of abstraction. A class is a container for relevant information and functions that work on data, thereby hiding intricate details of the implementation to the end-user. The user must concentrate on the function of a class instead of what it works.


Encapsulation is the main reason exist objects. Since the beginning, we’ve been discussing objects, their information, functions and privacy. It’s now time to find out how all this is held in check. The answer is encapsulation. Although it might seem like a capsule it’s pretty much identical. In this case, we attempt to bring the functions and data together that belong to the same category.


As we have seen in the previous tutorial, inheritance involves creating a set of fundamental properties and functions all in one place and then re-using them later by inheriting the class from which they were originally defined.

Python supports simpleMultiple and Multilevel inheritance. We will cover the details of these in our inheritance tutorials.


Polymorphism, also known as poly + morph is a term used to describe “many types”. In simple terms, Polymorphism is the characteristic of any function or operation which can act differently based on the input they receive.

Polymorphism can take place in two different ways They include:

Function Overloading

In OOP there is the possibility to create an operation that performs differently by overloading functions. What we need to do is to make different functions that share similar names but different parameters. For instance, let’s consider the function called include(), which adds all the parameters it has and returns the final result. In Python, we can define it as

define add(a, B) returns the a + b

It is capable of performing functions like:

>> add(4,5)

In the above example, an addition the function always takes two numbers as inputs But what happens do you do if you need to multiply 3 numbers at a time or perhaps four numbers.

Therefore it is possible to use OOP you simply have to create the functions add and again, but this time with three parameters. This method is referred to by the name of function overloading.

#3 to multiply 3 number using add(a, b, and c) Return the sum of a + b + # for adding 4 numbers using add(a, B, C, D) returning A + B + C + d

Now, we can invoke the the add() function with three, two or four parameters. You can observe the functions add() now has several types.

Operator Overloading

The operators you know are AdditionDivisionMultiplication and so on. Python can read operators in different ways based on the circumstances. For instance,

>>>print(2 + 5)

It will output 7. However, it will not do

>>> print( "hello" plus "world")

Gives you output "helloworld". It is evident the way the + operator when combined in conjunction with numbers can perform mathematical addition but when combined with strings, it does concatenation.

The multiplication operator can also behave differently depending on the type of data variables that it employed. For instance,

>>> print( 3*7)

Does Give, 21, as she plays the string

>>> print("hello"*3)

It gives "hellohellohello". It is another instance of overloading the operator.

You may alos like:

Why Is Python The Best Choice For Budding Data Analysts?

Leave a Reply

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