Basic Concepts of Object-Oriented Programming in Python

Started by NiveRoshni, Sep 01, 2020, 01:07 PM

Previous topic - Next topic



While learning Object-Oriented Programming. I decided to dive into its history and it turned out to be fascinating. The term "Object-Oriented Programming" (OOP) was coined by Alan Kay around 1966 while he was at grad school. The language called Simula was the first programming language with the features of Object-oriented programming. It was developed in 1967 for making simulation programs, in which the most important information was called objects.

Though OOPs were in the market since the early 1960s it was in the 1990s that OOPs began to grow because of C++. After that, this technique of programming has been adapted by various programming languages including Python Today its application is in almost every field such as Real-time systems, Artificial intelligence, and expert systems, Client-server systems, Object-oriented databases, and many more.

So, in this article, I will explain the basic concepts of Object-Oriented Programming in Python. It is important that you know Python before you continue. You can learn Python using the free course mentioned below-

Table of Content

1.What Is Object-Oriented Programming?
2.Object-Oriented Programming (OOP) vs Procedure Oriented Programming (POP)
3.Major Concepts of OOPs-
4.What is a class?
5.Object and object instantiation
6.Class methods
7.Inheritance in Python Class
10.Data abstraction

What Is Object-Oriented Programming?

Object-Oriented Programming(OOP), is all about creating "objects". An object is a group of interrelated variables and functions. These variables are often referred to as properties of the object and functions are referred to as the behavior of the objects. These objects provide a better and clear structure for the program.

For example, a car can be an object. If we consider the car as an object then its properties would be – its color, its model, its price, its brand, etc. And its behavior/function would be acceleration, slowing down, gear change.

Another example- If we consider a dog as an object then its properties would be- his color, his breed, his name, his weight, etc. And his behavior/function would be walking, barking, playing, etc.

Object-Oriented programming is famous because it implements the read world entities like objects, hiding, inheritance, etc in programming. It makes visualization easier because it is close to real-world scenarios.

Object-Oriented Programming (OOP) vs Procedure Oriented Programming (POP)
The basic difference between OOP and procedural programming is-

One way to think about POP is the same way you make lemonade for example. The procedure of making lemonade involves- first taking water according to the need, then adding sugar to the water, then adding lemon juice to the mixture, and finally mixing the whole solution. And your lemonade is ready to serve. In a similar way POP requires a certain procedure of steps. A procedural program consists of functions. This means that in the POP approach the program is divided into functions, which are specific to different tasks. These functions are arranged in a specific sequence and the control of the program flows sequentially.
Whereas an OOP program consists of objects. The object-Oriented approach divides the program into objects. And these objects are the entities that bundle up the properties and the behavior of the real-world objects.

POP is suitable for small tasks only. Because as the length of the program increases, the complexity of the code also increases. And it ends up becoming a web of functions. Also, it becomes hard to debug. OOP solves this problem with the help of a clearer and less complex structure. It allows code re-usability in the form of inheritance.
Another important thing is that in procedure-oriented programming all the functions have access to all the data, which implies a lack of security. Suppose you want to secure the credentials or any other critical information from the world. Then the procedural approach fails to provide you that security. For this OOP helps you with one of its amazing functionality known as Encapsulation, which allows us to hide data. Don't worry I'll cover this in detail in the latter part of this article along with other concepts of Object-Oriented Programming. For now, just understand that OOP enables security and POP does not.
Programming languages like C, Pascal and BASIC use the procedural approach whereas  Java, Python, JavaScript, PHP, Scala, and C++ are the main languages that provide the Object-oriented approach.

Major Python OOPs concept-
In this section, we will deep dive into the basic concepts of OOP. We will cover the following topics-

*Data Abstraction

1. What is a Class?

A straight forward answer to this question is- A class is a collection of objects.  Unlike the primitive data structures, classes are data structures that the user defines. They make the code more manageable.

class class_name:
    class body

2. Objects and object instantiation

When we define a class only the description or a blueprint of the object is created. There is no memory allocation until we create its object. The objector instance contains real data or information.

Instantiation is nothing but creating a new object/instance of a class. Let's create the object of the above class we defined-

obj1 = Car()

3. Class methods

So far we've added the properties of the car. Now it's time to add some behavior. Methods are the functions that we use to describe the behavior of the objects. They are also defined inside a class. Look at the following code-

class Car:   
    car_type = "Sedan"

    def __init__(self, name, mileage): = name
        self.mileage = mileage

    def description(self):                 
        return f"The {} car gives the mileage of {self.mileage}km/l"

    def max_speed(self, speed):
        return f"The {} runs at the maximum speed of {speed}km/hr"

description()- This method is returning a string with the description of the car such as the name and its mileage. This method has no additional parameter. This method is using the instance attributes.
max_speed()- This method has one additional parameter and returning a string displaying the car name and its speed.

4. Inheritance in Python Class

Inheritance is the procedure in which one class inherits the attributes and methods of another class.  The class whose properties and methods are inherited is known as Parent class. And the class that inherits the properties from the parent class is the Child class.

The interesting thing is, along with the inherited properties and methods, a child class can have its own properties and methods.

5. Encapsulation

Encapsulation, as I mentioned in the initial part of the article, is a way to ensure security. Basically, it hides the data from the access of outsiders. Such as if an organization wants to protect an object/information from unwanted access by clients or any unauthorized person then encapsulation is the way to ensure this.

You can declare the methods or the attributes protected by using a single underscore ( _ ) before their names. Such as- self._name or def _method( ); Both of these lines tell that the attribute and method are protected and should not be used outside the access of the class and sub-classes but can be accessed by class methods and objects.

Though Python uses ' _ ' just as a coding convention, it tells that you should use these attributes/methods within the scope of the class. But you can still access the variables and methods which are defined as protected, as usual.

Now for actually preventing the access of attributes/methods from outside the scope of a class, you can use "private members". In order to declare the attributes/method as private members, use double underscore ( __ ) in the prefix. Such as – self.__name or def __method(); Both of these lines tell that the attribute and method are private and access is not possible from outside the class.

6. Polymorphism

This is a Greek word. If we break the term Polymorphism, we get "poly"-many and "morph"-forms. So Polymorphism means having many forms. In OOP it refers to the functions having the same names but carrying different functionalities.

7. Data abstraction

We use Abstraction for hiding the internal details or implementations of a function and showing its functionalities only. This is similar to the way you know how to drive a car without knowing the background mechanism. Or you know how to turn on or off a light using a switch but you don't know what is happening behind the socket.

Any class with at least one abstract function is an abstract class. In order to create an abstraction class first, you need to import ABC class from abc module. This lets you create abstract methods inside it. ABC stands for Abstract Base Class.



Thank you so much for sharing all this wonderful information !!!! It is so appreciated!! You have good humor in your blogs. So much helpful and easy to read! Python could be a completely practical programming language that will do something virtually the other language can do, at comparable speeds. Python is capable of threading and GPU processing just like the other language. Most of the information process modules are literally simply Python wrappers around C/C++ code.


Thank you so much for sharing all this wonderful information !!
Convenience of packaged goods does not necessarily mean single-use servings, and it is not exclusively designed for people who are always on the move.

Quick Reply

Note: this post will not display until it has been approved by a moderator.

Please leave this box empty:
Type the letters shown in the picture
Listen to the letters / Request another image

Type the letters shown in the picture:
Shortcuts: ALT+S post or ALT+P preview