Python Instance, Class, and Static Methods Explained
source link: https://medium.com/sanrusha-consultancy/explained-instance-class-and-static-methods-in-python-2df481c3f022
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Python Instance, Class, and Static Methods Explained
It’s easier than you thought!
In an Object-Oriented Programming (OOP) like Python, class, method, instance, object, play a critical role. In this article, I will take you through different method types used in Python OOP.
Figure 1 explains the Instance Method, Class Methods, and Static Methods in Python. While the Instance method is the most common method Class method and Static method are decorators (note @ sign).
Instance Method
Figure2 explains the Instance Method of Python.
The below code is an example of the Instance Method. Here the class Car contains __init__ method and self parameter.
This class is just a blueprint. To use this class, it has to be instantiated i.e. object should be created like below.
toyotaCamry=Car('Toyota','Camry','2007')
audiQ3=Car('Audi','Q3','2015')
Two objects toyotaCamry and audiQ3 has been created by passing appropriate parameter values to class Car instance. Now, these two objects can be used.
If you want toyotaCamry details, call Instance method details through the object toyotaCamry
toyotaCamry.details()
Output:
Car Details
Make Toyota
Model Camry
Year 2007
This is how the instance method works.
Class Method
A class method is a decorator. A class method does not need object and can access and manipulate the class attributes directly through the class.
Figure 3 explains the class method.
The below code is an example of a class method. Here the instance class with __init__ method and self parameter is not required. A class method can exist in any class with or without attributes.
Below class, Car contains attributes make, model, and year. These attributes are accessible through the class method pricerange. Class method need a mandatory parameter cls.
If you just call Car.pricesrange it works. No need to create an instance and object of class Car.
Car.pricerange()
Output:
Medium Price Range
You can also assign values to class attributes. Below the make of car is assigned the value Audi and price range method is called.
Car.make='Audi'
Car.pricerange()
Output changes to Expensive Car
Expensive Car
Static Method
Like class method, a static method is also a decorator. Unlike the class methods, a static method cannot access class attributes. It can access only the values passed in its parameter.
Figure 4 explains the Static Method.
Below is an example of a static method. The static method affordability can access only the parameter value make.
Usually, the static method is used only within the class. However, if you need, you can directly call the static method through class without creating any object.
Car.affordability('Audi')
Output:
'Luxury'
You can also call the static method through object
audiQ3=Car('Audi','Q3','2015')
audiQ3.affordability('Toyota')
Output:
'Affordoable'
That’s it. That’s all about Instance, Class, and Static Methods in Python. I hope it helped you understand the concept.
Looking forward to your questions and feedback!
Reference:
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK