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 variables, data types, conditional 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.
class MyName: pass
Let me explore this class with constructor and function or method to define in this class.
class MyName: my_name = "Sunil gupta" def __init__(self, name): print("This is the constructor.") self.name = name def getName(self): # Reference the name print(self.name + " is here.") def getAge(self, age): print("My age is " + str(age))
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.
def main(): # Set name of MyName object name = MyName("Sunil") # creating the object of MyName print("My Name is",name.my_name) # call the class variable name.getName() # call the function of call on object reference if __name__ == "__main__": main()
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.
class BaseClass: # statement class DerivedClass(BaseClass): # statement
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.
class MyName: my_name = "Sunil gupta" def __init__(self, name): print("This is the constructor.") self.name = name def getName(self): # Reference the name print(self.name + " is here.") def getAge(self, age): print("My age is " + str(age)) class ChildName(MyName): def __init__(self, name): self.name = name print("This is the child constructor.") super().__init__(self) def get_super_name(self): print(self.my_name, "child")
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.
def main(): # Set name of MyName object name = MyName("Sunil") print("My Name is",name.my_name) name.getName() child_name = ChildName(name) print("My child Name is",child_name.my_name) child_name.get_super_name() name.getAge(20) # get the function call on object reference child_name.getAge(30) # get the super class function call on child object reference
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.
class MyBaseClass(object): def __init__(self): super().__init__() print('base class') def mymethod(self): print('method in MyBaseClass') class MyBaseClass2(object): def __init__(self): super().__init__() print('base class 2') def mymethod(self): print('method in MyBaseClass2') class MyDerivedClass(MyBaseClass, MyBaseClass2): def __init__(self): super().__init__() print('Derived class')
Here MyDerivedClass(MyBaseClass, MyBaseClass2)the
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.
drived = MyDerivedClass() drived.mymethod()
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. If you learn all python article then learn from the python category.
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 the 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