Instance Method
- They are methods that are tied to the instance of an class
- They take
self
as their first parameter (Self refers to the current instance of the class)
- They can modify all the attributes of the instance of the class
- They are called using the instance name :
instance_name.function_name()
Class Method
- They are methods that are tied to the class (Not to a instance)
- They take
cls
as their first parameter (Cls refers to the class in which the function is declared)
- Class Methods have to be decorated using the
@classmethod
decorator
- They can modify the class attributes
- They are called using the class name :
class_name.function_name()
- Class Methods are generally used to declare alternate constructors in a class as Python does not support constructor overloading
Static Methods
- They are methods that are not tied to the class or instance of the class
- They do not take any special parameters are arguments
- Static Methods need to be decorated using the
@staticmethod
decorator
- They can access the class attributes using the class name by this approach is not recommended
- They are called using the class name :
class_name.function_name()
- When we don’t have to access the class attributes and instance attributes we should used the static method