A static method is a method that belongs to a class, but:
-
does not depend on object data ( self )
-
does not depend on class data ( cls )
-
behaves like a normal function, but is grouped inside a class for better organization
Static methods are used when a method is logically related to a class, but does not need to access or modify class or instance properties.
Example :
Python
class Calculator: @staticmethod def add(a, b): return a + b print(Calculator.add(5, 3)) #output 8 Calling Static Methods :
Python
ClassName.method_name() Python
c = Calculator() print(c.add(2, 4)) # works, but avoid Static Method Inside Class :
Python
class User: @staticmethod def is_valid_age(age): return age >= 18 print(User.is_valid_age(20)) print(User.is_valid_age(15)) #output True False