In Python, both classmethod
and staticmethod
are used to define methods that belong to a class rather than an instance of the class. However, there are some differences between the two. In the previous article, I told you how to create class method in python class with examples. In this article, I will tell you how to define static method in a python class, and the difference between python class method and static method.
1. How To Create Static Method In Python Class.
1.1 How To Define Static Method In Python.
- In Python, the
staticmethod()
function is used to define a static method. - A static method is a method that belongs to a class, but does not have access to the class or instance itself.
- A static method can be called on the class itself or on an instance of the class.
- To define a static method using the
staticmethod()
function, we need to use the@staticmethod
decorator before the method definition. - The method does not take any special parameters like
cls
orself
. - Here is the syntax for defining a static method using the
staticmethod()
function:class MyClass: @staticmethod def my_static_method(arg1, arg2, ...): # Method code here
- In the above syntax, my_static_method is the name of the static method, and arg1, arg2, etc. are the parameters that the method takes.
1.2 How To Use Python Static Method.
- Here is an example of defining and using a static method.
class MyClass: @staticmethod def my_static_method(x, y): return x + y result = MyClass.my_static_method(3, 5) print(result) # Output: 8
- In this example, we define a class
MyClass
with a static methodmy_static_method
using the@staticmethod
decorator. - The
my_static_method
method takes two parametersx
andy
and returns their sum. - We can call the
my_static_method
method on the class itself, rather than on an instance of the class. - Note that unlike instance methods and class methods, static methods do not have access to the class or instance itself.
- They are useful for grouping utility functions that do not depend on the class or instance.
2. What Is The Difference Between Classmethod And Staticmethod In Python.
classmethod
andstaticmethod
are two types of methods in Python that can be used to define methods inside a class.- The main difference between them is in the way they are called and how they handle arguments.
2.1 classmethod.
- It is a method that belongs to the class and not the instance of the class.
- It takes the class (cls) as the first argument instead of the instance (self).
- It can access and modify the class state, which means it can modify class-level variables or call other class methods.
- It is commonly used to create alternative constructors for a class.
- Example:
class MyClass: count = 0 def __init__(self): MyClass.count += 1 @classmethod def get_count(cls): return cls.count obj1 = MyClass() obj2 = MyClass() print(MyClass.get_count()) # Output: 2
2.2 staticmethod.
- It is a method that belongs to the class and not the instance of the class.
- It does not take any special first argument like
cls
orself
. - It cannot modify the class state or access class-level variables.
- It is commonly used for utility functions that do not depend on the state of the object.
- Example:
class Math: @staticmethod def add_numbers(x, y): return x + y print(Math.add_numbers(2, 3)) # Output: 5
2.3 Conclusion.
- In summary,
classmethod
is used to modify or access the class state, whilestaticmethod
is used for utility functions that do not depend on the state of the object.