@staticmethod and @classmethod are two different decorators used to define methods inside a class. The main difference between them is how they handle the first parameter of the method @staticmethod is used to define a method that does not take any special first argument. It is similar to a regular function outside of a class, but is defined inside a class.
@staticmethod does not receive any reference to the instance of the class, nor to the class itself. @classmethod is used to define a method that takes a reference to the class itself as its first argument. This first argument is usually called
cls by convention, instead of self. This allows the method to access the class and its attributes. class MyClass: @staticmethod def my_static_method(x, y): return x + y
result = MyClass.my_static_method(3, 5) print(result) # Output: 8 class MyClass: class_variable = 10
@classmethod def my_class_method(cls, x): return cls.class_variable + x
result = MyClass.my_class_method(5) print(result) # Output: 15
We use the @staticmethod and @classmethod decorators in Python to define methods within a class, but each of them has different purposes and uses.
The @staticmethod decorator is used to define a method that does not access or modify the state of an instance, and a special first argument is not passed. It is similar to a regular function that is defined outside the class, but it is within the scope of the class and can be called using the class name or an instance of the class.
class Class1:
@staticmethod
def static_method():
print("This is a static method")
# Call the static method using the class name
Class1.static_method()
# Call the static method using an instance of the class
obj = Class1()
obj.static_method()
# Output = This is a static method
This is a static method
The @classmethod decorator is used to define a method that operates on the class and receives the class itself as the first argument, which is conventionally named cls. This method can access and modify the state of the class or create new instances of the class. It can also be called using the name of the class or an instance of the class.
class Class2:
count = 0
def __init__(self):
Class2.count += 1
@classmethod
def get_count(cls):
print("The count is:", cls.count)
# Create instances of the class
obj1 = Class2()
obj2 = Class2()
# Call the class method using the class name
Class2.get_count()
# Call the class method using an instance of the class
obj1.get_count()
# Output = The count is: 2
The count is: 2
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
@staticmethod and @classmethod are two different decorators used to define methods inside a class. The main difference between them is how they handle the first parameter of the method
@staticmethod is used to define a method that does not take any special first argument. It is similar to a regular function outside of a class, but is defined inside a class. @staticmethod does not receive any reference to the instance of the class, nor to the class itself.
@classmethod is used to define a method that takes a reference to the class itself as its first argument. This first argument is usually called cls by convention, instead of self. This allows the method to access the class and its attributes.
class MyClass:
@staticmethod
def my_static_method(x, y):
return x + y
result = MyClass.my_static_method(3, 5)
print(result) # Output: 8
class MyClass:class_variable = 10@classmethoddef my_class_method(cls, x):return cls.class_variable + xresult = MyClass.my_class_method(5)print(result) # Output: 15We use the @staticmethod and @classmethod decorators in Python to define methods within a class, but each of them has different purposes and uses.
The @staticmethod decorator is used to define a method that does not access or modify the state of an instance, and a special first argument is not passed. It is similar to a regular function that is defined outside the class, but it is within the scope of the class and can be called using the class name or an instance of the class.
This is a static method
The @classmethod decorator is used to define a method that operates on the class and receives the class itself as the first argument, which is conventionally named cls. This method can access and modify the state of the class or create new instances of the class. It can also be called using the name of the class or an instance of the class.