Python OOPS concept with Class, Object and Constructor

In the python tutorial series, we have understood the basics of how to get started to start working in python development.  In my last series, we have completed the concept of variablesdata typesconditional control, functional control,  Module, and packages and Data Structure (List, Dictionary, and Set) of python.

Before starting this tutorial I would be recommended to check my last tutorials which are mentioned in above links or you can check from python category to find all python related tutorial that will help you to make strong in python development.

Now in this tutorial series, we will learn about the Class and Object of python. So let us get started to understand the what is Class and Object in python. As we know and familiar with Class and Object of Java. In python also it has the same concept nothing difference in concept about the Class and Object. These are two main feature of Object-oriented programming.

  • Class — A blueprint created by a programmer for an object. This defines a set of attributes that will characterize any object that is instantiated from this class.
  • Object — An instance of a class. This is the realized version of the class, where the class is manifested in the program.

We can define the class  with class keyword with any name and end with: let’s check the definition below.

Let me explore this class with constructor and function or method to define in this class.

As we know that every class has properties and behaviors it means a class has variables and function definitions. Now in python, few things or statements are different in compare with java. Python has __init__  and self  which are defined in the above code. We will discuss these two keywords in detail.

The __init__ method is roughly what represents a constructor in Python. When you call MyName("Sunil")Python creates an object for you, and passes it as the first parameter to the __init__ method. Any additional parameters (e.g., MyName("Sunil", 30)) will also get passed as arguments–in this case causing an exception to be raised since the constructor isn’t expecting them. Here is the reference for detail.

self variable represents the instance of the object itself. Most object-oriented languages pass this as a hidden parameter to the methods defined on an object; Python does not. You have to declare it explicitly. When you create an instance of the MyName class and call its methods, it will be passed automatically.

Let’s define the main function to create the Object of the Class and call the function.

Output:

This is the constructor.
My Name is Sunil gupta
Sunil is here.

Inheritance

Inheritance is a major key role of python oops programming. Inheritance is when a class uses code constructed within another class. Classes called child classes or subclasses inherit methods and variables from parent classes or base classes.

Let’s see how can we define the inheritance class in python.

DerivedClass(BaseClass) this statement is representing the inheritance. It means DerivedClass inherited the property and behavior of BaseClass. Let’s see an example to clearly understand.

Here ChildName(MyName) statement ChildName class is the subclass of MyName Class. It means we can access the function and variable of MyName class inside the ChildName class. Let’s see to create the instance of  ChildName to access the MyName functions and variables.

Output:

This is the constructor.
My Name is Sunil gupta
Sunil is here.
This is the child constructor.
This is the constructor.
My child Name is Sunil gupta
Sunil gupta child
My age is 20
My age is 30

Multiple Inheritance

python support the multiple inheritance concepts. It means that any derived class can inherit the multiple BaseClass properties and behaviors.  Let’s see an example to clearly understand this concept.

Here MyDerivedClass(MyBaseClass, MyBaseClass2)statement represents that MyDerivedClass inherit the properties and behaviors of MyBaseClass and MyBaseClass2.

Let’s see to create the MyDerivedClass object and access the parent class function. Here is the function call.

Output:

base class 2
base class
Derived class
method in MyBaseClass

Summary: Now we have a good understanding of the Class and Object of python. We have seen few examples and play around it to clear the doubts. If you still having issue or doubts then please add a comment I will try to reply as much possible. In my next python tutorial series, we will learn about the Files of python with the example.

If you are wondering to learn Android then Please Learn from Android category and Wondering to lean Kotlin then Kotlin Category will help you.

Please do subscribe your email to get the newsletter on this blog on below and if you like this post then do not forget to share like and comment on below section.

Happy coding 🙂

I am a very enthusiastic Android developer to build solid Android apps. I have a keen interest in developing for Android and have published apps to the Google Play Store. I always open to learning new technologies. For any help drop us a line anytime at contact@mobologicplus.com

Leave a Reply

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