Table of contents
No headings in the article.
There are three types of methods: Instance Method, Class Method, and Static Method
So today we will look at CLASS METHODS & STATIC METHODS in python
What is the class method?
- Class methods are those methods that act upon the class variables or static variables (Declaring a variable inside a class but outside of the method) of the class.
Now, let's jump into how to make the class method :
- Firstly, we will need to specify Decorator @classmethod which is used to write the class method. Next is to write a parameter by default the first parameter of the class method is "cls" which refers to the class itself.
Note: A decorator is a plan design in Python that permits a client to add new usefulness to a current program without changing its construction or simply adding new functionalities to the class or function
Now, let's look
Class Method Without Parameter
For example, let us create a class called Animal where we will add the decorator and then will define our class method which is 'show_category' with default parameter (cls) included in it.
Calling class method without Parameter/Arguments
Syntax to call a class method is : Classname.method_name()
class Animal:
@classmethod
def show_category(cls):
print("Elephant")
Elephant=Animal()
Animal.show_category() #calling class method w/o argument
Class Method with Parameter/Arguments and calling the class method with parameters
Here we will pass on some arguments for the method like color, walk, etc and the syntax for calling a method with the argument is :
Syntax : Classname.method_name(Actual_Argument)
So,
class Animal:
walk="yes" #class variable
@classmethod #decorator
def show_category(cls,color,num_of_legs): #defining method with parameter
cls.color=color
cls.num_of_legs=num_of_legs
print("Can walk? : ", cls.walk)
print("Color : " ,cls.color)
print("Number of legs : ", cls.num_of_legs)
Elephant=Animal() # making object
Animal.show_category("black" , "4")
Output :
What is Static Method?
Static methods are used when some processing is related to the class but no need for class or its perform any work. We use the static method when we want to pass some values inside but outside of methods.
Decorator @staticmethod is used to write the above static method
Static Method Without Parameter and calling static method without Argument
There is no default argument here.
The syntax for calling Static method : Classname.method_name()
Let us take an example where we create a class called Mobile then write a static decorator and define the method that is "show_model()".
class Mobile:
finger_print="Yes"
@staticmethod #static decorator
def show_model():
print(Mobile.finger_print)
Oppo=Mobile()
Mobile.show_model() #calling class method w/o argument
Static Method With Parameter and calling a static method with Argument
class Mobile:
finger_print="Yes"
@staticmethod
def show_model(m, p):
model=m
price=p
print("Model : " , model , "Price : " , price)
Oppo=Mobile()
Mobile.show_model("Oppo A83" , "15000") #calling static method
In the above code, we can see that whenever a function is called the control from calling the static method is taken to define a method where the values of model name and price i.e oppo A83 is given to parameter 'm' and 15000 to parameter ' p ' respectively, then whatever is there in parameter 'm' is assigned to model and same for parameter 'p' to price.
Summing up the key points