Python: Instance, Class and Static Methods

Python: Instance, Class and Static Methods

A clear and concise explanation about the differences between instance, class and static methods in Python.


Instance Method

An instance method is used to hide an object's internal state (known as attributes) while mediating any modifications other objects wish to make on it.

In Python, they are functions defined inside a class definition that take self as the first parameter. The self parameter refers to the instance itself and is used to access its own attributes and methods.

class Car:
    
    def drive(self):
        return "I'm driving this car."        

In practice:

>>> car = Car()
>>> car.drive()

"I'm driving this car."        

The class that originated this instance can also be accessed, through self.__class__, enabling instance methods to access class attributes:

class Car:
    
    nr_cars = 0
    
    ...
    
    def destroy(self):
        self.__class__.nr_cars--        

In practice:

>>> car = Car()
>>> Car.nr_cars
1

>>> car.destroy()
>>> Car.nr_cars
0        

Intuitively, instance methods cannot be called directly from a class, because they must be associated with an instance:

>>> Car.drive()

TypeError: unbound method drive() 
must be called with Car instance as 
first argument (got nothing instead)        


Class Method

A class method is used to access/modify just the class state, they do not refer to any specific instance and so there's no reason to pass the self parameter to it. Instead, it takes cls as the first parameter, that refers to the class itself and is used to access class attributes.

In Python, they are functions defined inside a class definition, decorated with the classmethod decorator and take cls as the first parameter.

class Car:

    nr_cars = 0
    
    ...

    @classmethod
    def nr_cars_produced(cls):
        return cls.nr_cars

    @classmethod
    def close_down_factory(cls):
        cls.nr_cars = 0        

In practice:

>>> Car()
>>> Car.nr_cars_produced()
1        

You might be surprised to learn that, in Python, an instance can also call class methods.

>>> car = Car()
>>> car.nr_cars_produced()
1        

This is discouraged because it is not immediately clear, to someone reading the code, we're calling a class method!


Static Method

A static method is intended to perform computations that do not modify any object or class state, so there's no reason to pass either self or cls parameters to it.

In Python, they are functions defined inside a class definition, decorated with the staticmethod decorator.

class Car:
    
    @staticmethod
    def newsletter():
        return "This month's newsletter about cars."        

In practice:

>>> Car.newsletter()

"This month's newsletter about cars."        

You might be surprised to learn that, in Python, an instance can also call static methods.

>>> car = Car()
>>> car.newsletter()

"This month's newsletter about cars."        

This is discouraged because it is not immediately clear to someone reading the code, we're calling a static method!


Tags:

要查看或添加评论,请登录

社区洞察

其他会员也浏览了