What is Object Oriented Programming in Python?

What is Object Oriented Programming in Python?

(2) IBM Research Chief Scientist Ruchir Puri On Quantum Computing And No Code Machine Learning
Object-oriented programming is a method of structuring a program by bundling related properties and behaviors into individual objects.

It is a programming paradigm providing a means of structuring programs. So that properties and behaviors become bundled into individual objects.

Take a car for example. Things like color, year of manufacture, and manufacturer are properties of the car, while braking, acceleration, and so on are behaviors of the car.

Also, we can say that object-oriented programming is an approach for modeling concrete, real-world things, like cars, as well as relations between things, like companies and employees, students, and teachers, and so on. Object-oriented programming models real-world entities as software objects that have some data associated with them and can perform certain functions.

In Python, Object-oriented programming is a programming paradigm that uses objects and classes in programming, which follows the following principles: classes, objects, methods, inheritance, polymorphism, data abstraction, and encapsulation. The main concept of Object-oriented programming is to bind the data and the functions that work on that together as a single unit so that no other part of the code can access this data.

Four main pillars of object-oriented programming

data abstraction

Abstraction hides the internal functionality of the application from the user.  Users can be end customers or other developers.  

We can find abstractions in everyday life.  For example, you know how to use your phone, but you may not know exactly what’s going on inside the phone every time you open an app.  

Another example is Python itself.  You know how to use it to build functional software, even if you don’t understand the inner workings of Python.  

Applying the same code allows you to collect all the objects in the problem and abstract the standard functionality into classes. 

inheritance

Inheritance allows us to define multiple subclasses from a defined class.  

Its main purpose is to follow the DRY principle.  By implementing all shared components into a superclass, you will be able to reuse a lot of code.  

You can think of it as the concept of genetic inheritance in real life.  A child is the result of inheritance between two parents.  They inherit all physical properties (properties) and some common behaviors (methods). 

polymorphism

Polymorphism allows us to slightly modify the methods and properties of subclasses previously defined in the superclass.  

Literally means “many forms”.  That’s because we built methods with the same name but different functionality.  

Going back to the idea, children are also a perfect example of polymorphism.  They can inherit the defined behavior ‘get_hungry()’ but in slightly different ways, for example, starving every 4 hours instead of every 6 hours. 

encapsulation

Encapsulation is the process by which we protect the internal integrity of data in a class.  

Although there is no private statement in Python, you can apply encapsulation by using Mangling in Python.  There are special methods called getters and setters that allow us to access unique properties and methods.  

Let’s imagine a Human class that has a unique attribute called _height.  You can only modify this property within certain constraints (hardly higher than 3 meters). 

Written by Anyu Mei

Back To News

What is Object Oriented Programming in Python?