In Python, classes can have three different types of methods. Such as
instance methods, static methods and class methods.
We have already seen instance methods in previous lessons.
In this lesson you learn about static and class methods.
static methods and class methods
Unlike instance methods, static and class are not bound to instance of a class.
They can be called without creating an instance of a class.
Class methods are used to work with class attributes.
They can be called without creating an instance.
similar to instance methods classmethods take a reference to class,
as first argument. Usually ‘cls’ is used as class reference.
Class methods are created, by using @classmethod decorator
Creating Class methods
class Employee(object): @classmethod def i_am_class_method(cls): pass
Notice ‘cls‘ reference passed while defining it.
Python implicitly passes ‘cls’ reference when classmethod is called.
Calling class methods
Class methods can be called by using class name or instance.
Employee.i_am_class_method() or emp_1 = Employee() emp_1 .i_am_class_method()
A static method is like a normal module level function, defined inside a class.
But they never receive an automatic self, or cls argument.
Static methods are also can be called by using class name or instance.
static methods are created, by using @staticmethod decorator
Creating static methods
class Employee(object): @staticmethod def i_am_static_method(): pass
Calling static methods
Static methods can also be called by using class name or instance.
Employee.i_am_static_method() or emp_1 = Employee() emp_1.i_am_static_method()
Generally static methods are used to,
perform utility functions related to class.
Below is an example with both of these methods.
class Employee(object): num_of_empoyee = 0 def __init__(self): Employee.num_of_empoyee += 1 @classmethod def i_am_class_method(cls): print("classmethod : num_of_employee :",cls.num_of_empoyee) @staticmethod def i_am_static_method(): print("staticmethod : do general activity ") emp_1 = Employee() emp_2 = Employee() emp_3 = Employee() Employee.i_am_class_method() Employee.i_am_static_method() output: classmethod : num_of_employee : 3 staticmethod : do general activity
In the above example Employee class, num_of_employee is a class level attribute.
__init__() a special method initializes class objects
i_am_class_method() is a class method defined using a @classmethod decorator.
i_am_static_method() is static method defined using a @staticmethod decorator.
i_am_class_method is designed to process class data,
not the instance data.
num_of_employee attribute is shared across all the instances of the class.
In true sense, static methods are not of much use is in Python.
It only make sense to have them in class definition for better
Otherwise functionality can be achieved,
through any normal module level function.
On a side note, static methods are similar to class methods.
Whatever functionality you can achieve through class methods,
can be achieved through static methods.
Below is a guideline to choose between these two.
Choosing between static and class method
Choose static methods
1. When there is no need of accessing either instance or class attributes.
2. While presenting implementation detail of the class.
3 And it can be move outside and can became a module level function
Choose class methods
1. While processing class level attributes
2. For creating named constructors( factory functions)
In the next lesson you learn about Encapsulation.