Is Python Object-Oriented Programming Language?

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Yes, Python is an objected oriented programming language. Everything in python is an object. Using python we can create classes and objects, for example, that functions are first-class objects. Functions, classes, strings, and even types are objects in Python: like any object, they have a type, they can be passed as function arguments, and they may have methods and properties. Therefore, Python is object oriented programming language.

Main Concepts Of Object-Oriented Programming

Object-oriented programming is a method of structuring a program by bundling related properties and behavior into individual objects. These objects are related to entities. Object-oriented programming language helps us in writing reusable code. It is a popular and widely used method of solving problems by creating objects.

For example let's take humans as a class. The height, weight, color are all attributes of an object. Biological Processes like Breathing, Walking, and Eating all are considered Methods with data and logic.

Object-oriented Programming (OOP) is a computer programming model that helps one organize their software design around data, class, and object rather than functions and logic.

Let us think of a Clothing factory where each step of the line processes some material and transform it into a finished product.

Classes and objects are the two main building blocks of object-oriented programming. A class creates a new type of object where objects are instances of the class.

Class

Class is the blueprint used to create a user-defined data structure. It holds the common attribute and behaviors of the object. For example, the class “Cloth” will hold objects “Shirt”, “Pant” and “Tshirt”`.

For example, let us say that we want to store information on clothes, the general approach would be to store all the information on a list.

There are several issues with this approach.

First, it can make larger code files more difficult to manage. If you reference pant[0] several lines away from where the pant list is declared. There may be a case when we forget the index for the required value.

Second, it can introduce errors if not every cloth has the same number of elements in the list. In the third list above, the company is missing, so the tshirt[2] will return 1200 instead of the t-shirt's company name.

A great way to make this type of code more manageable and more maintainable is to use classes.

All class definitions start with the class keyword, which is followed by the name of the class and a colon. Any code that is indented below the class definition is considered part of the class’s body.

Here's an example of a Cloth Class :

  • Note: Python class names are written in CapitalizedWords notation by convention. For example, a class for a specific brand of cloth like the John Players would be written as JohnPlayers.

Currently, our class doesn't do much. We need to manage the properties like size, price, name, and company name. We define those properties using a method called init(). Every time a new cloth object is created init() sets the initial state of the object by assigning the values of the object's properties.

You can give init() any number of parameters, but the first parameter will always be a variable called self. When a new class instance is created, the instance is automatically passed to the self parameter in init() so that new attributes can be defined on the object.

Let's update the cloth class with init() method that create .name , .size , .price , .company_name attributes:

Using such a Class creating and managing data seems quite easy

shirt = Cloth("shirt", 25 , 1200 , "xyz") By using such a syntax we can retrieve each value as follows :

Object

An object is an instance of a class. It has both a property (variables) and behavior (methods). The python object “Cloth” will have properties; “buy”, “about”, “sell” and behaviors; “discount”, and “show”.

Everything in Python is an object, and almost everything has attributes and methods. All functions have a built-in attribute doc, which returns the docstring defined in the function source code.

Method

Methods are the functions that are defined inside a class and can only be called from an instance of that class. A method includes the operation and functionality of the object. These methods are defined inside a class. These methods are reusable piece of code that can be invoked/called at any point in the program.

Here the show is a method or functionality to print all the data being passed at the time of object creation.

Inheritance

Inheritance is the process by which one class takes on the attributes and methods of another. Newly formed classes are called child classes, and the classes that child classes are derived from are called parent classes.

It specifies that the child object acquires all the properties and behaviors of the parent object.

Python Inheritance

Inheritance has many advantages some of them are :

  • It provides the reusability of the code.
  • It provides Transition and Readability.
  • Real World Relationship.

Here we can notice that despite creating an Object of the child class we have access to the method from a different class. Since we know that methods are restricted to their instance, here we have access to them. This is due to inheritance as the Child class has the attribute of the Parent class.

Inheritance also overrides the init method. To avoid that we need to declare the Parent init function under the child init Method.

Parent.init(self,name,wish)

This is how we can override the Parent init function in the child function.

Polymorphism

Polymorphism contains two words "poly" and "morphs". Poly means many, and morph means shape. By polymorphism, we understand that one task can be performed in different ways. It's the ability to use a common interface for multiple forms or data.

Now in this example, we have created two classes “Cat” and “Dog”. They’re different animals and make different sounds. So, the make_sound() function should produce two different outputs based on the objects we pass through them. In this case, we have created two objects “cat1” and “dog1”.

The make-sound() function is producing two different outputs- “Meow” and “Bark”.

This concept in Object Oriented Programming is called Polymorphism. We can also consider this as Method Overloading.

Let's Create two classes and have some similar properties associated with them:

Here we can notice that we have two classes Phone and Watch which have two same methods send_msg and switch_off. Now to call these methods we have to call them separately.This may look tedious to do, well we have an easy way to implement the solution of the above problem as follows:

Now lets create two instances of the Object class

Besides calling both the methods differ for different classes, we pass that into the common interface that we created above.

So the final code goes as follows:

With the following code the given output is as follows:

So we can see that besides calling the same method to a different class that is from a different instance, we wrap those methods into a common interface and pass the instance as parameters.

Encapsulation

Encapsulation is also an essential aspect of object-oriented programming. It is used to restrict access to methods and variables.

Encapsulation refers to the wrapping up of data under a single unit. It acts as a protective shield for methods and function.

Encapsulation has some benefits, some of which are :

  • Data Hiding
  • Flexibility
  • Reusability

This gives the following as output : OOPS Python

Till now everything seems working. Let's make the language attribute a private attribute by naming convention by using a double underscore at first like __attribute-name.

This makes the attribute only accessible within the scope of the class for the implementation.

Let's have a look at the code.

Language makes the attribute language private to its scope. Also, there are ways to recall the value stored in language Attribute by the following code:

The above code has the following output : Python

Another use-case could be that we want to update a certain value in an attribute but we don't want to make that attribute accessible outside the scope.

Let's portray this by the following code :

Data Abstraction

Data abstraction and encapsulation are often used as synonyms. Both are nearly synonyms because data abstraction is achieved through encapsulation.

Abstraction refers to hiding unnecessary details to focus on the whole product instead of parts of the project separately. It is a mechanism that represents the important features without including implementation details. Abstraction helps us in partitioning the program into many independent concepts so we may hide the irrelevant information in the code. It offers the greatest flexibility when using abstract data-type objects in different situations.

Let us take the example of a Mobile. It has a screen, camera, body, etc. All these things combine to form a fully functional mobile, which is an abstraction, and all the different parts are its layers of abstraction. Now a screen is composed of various parts such as a display, touch module, screen guard, etc. For these flayers, the screen is an abstraction. In simple words, abstraction can be achieved by hiding the background details and showing only the necessary ones. In programming, abstraction can not be achieved without Encapsulation.

  • Abstract Class - Which has one or more Abstract Methods.
  • Abstract Methods - Methods with the declaration but not the definition.
  • Abstract classes can not be instantiated, and require subclasses to provide implementations for the abstract methods.
  • Subclasses of an abstract class in Python are not required to implement abstract methods of the parent class.

Here when we create an instance of the Child class and call a method over that it first goes to the child class display method which is extended from the Parent class. Inside the parent class, we have displayed it as an abstract class means the implementation is not present in the particular class. Therefore it's overridden by the child class.

This is how we implement a large codebase where the logic is separated into different classes for more organized and readable code.

Let's understand with another example :

Below we have made an abstract class named Mobile where we have an abstract method named switch_on. The method's implementation is not given in the class. Later on Another child class of Mobile named Screen with extended Mobile class, we have switch_on method implementation.

When the switch_on method is called over a power instance, its points to the switch_on method from the Screen class where the implementation is executed.

Learn more

Conclusion

  • Class is the blueprint of an object. It is used to declare and create objects. Class is a logical entity. Example: Computer is a class.
  • Object is an instance of the class. The object is a physical entity. And we can create as many objects as we want. Example: Mouse, Keyboard, Monitor, etc are objects of the class Computer.
  • Inheritance is an OOP concept, where existing classes can be modified or overridden by a new class. The existing class is called the base class or parent class and the new class is called the derived class or child class.
  • Polymorphism in OOP allows an object to take many forms. Simply, polymorphism allows us to perform the same action in many different ways. It's the ability to use a common interface for multiple forms or data.
  • Encapsulation in OOP is the process of restricting access to methods and variables. Encapsulation refers to the wrapping up of data under a single unit. It acts as a protective shield for methods and function.
  • Abstraction refers to hiding unnecessary details to focus on the whole product instead of parts of the project separately. Abstraction is done by Encapsulation.