Learning Sections show
@staticmethod
In Python, @staticmethod
is a decorator used to define a static method within a class. Static methods are methods that are bound to the class itself rather than to instances of the class. They do not require access to instance variables and can be called using the class name.
Example:
class Math:
# Static method to add two numbers
@staticmethod
def add(x, y):
return x + y
# Call static method using class name
print(Math.add(5, 3)) # Output: 8
Static methods are often used for utility functions related to the class that do not require access to instance data. They are defined using the @staticmethod
decorator to indicate that they do not operate on instance data.