65 - Static Methods

Static methods in Python are methods that belong to a class rather than an instance/object of the class.
They are defined using the @staticmethod decorator and do not have access to the instance of the class (i.e. self).
They are called on the class itself, not on an instance of the class.

Static methods are often used to create utility functions that don't need access to instance data but we wanna share it with the class.

class Math:
	def __init__(self,name,age):
		self.name= name
		self.age= age
		
    @staticmethod
    def add(a, b):      # doesn't need self argument
        return a + b

# calling with object:
o1= Math(1,2)
print(o1.add(1, 2))
#calling without object
print(Math.add(1, 2))

# Output
3

In this example, the add method is a static method of the Math class.
It takes two parameters a & b and returns their sum.

The method can be called on the class itself, without the need to create an instance of the class.