8 months, 3 weeks

Descriptors




 Descriptors are the basis of complex attribute access in Python. They are used internally to implement properties, methods, class methods, static methods, and super. They are objects that define how attributes of another class can be accessed. In other words, a class can delegate the management of an attribute to another class.

The descriptor classes are based on three special methods that form the descriptor 
protocol:
• __set__(self, obj, value): This is called whenever the attribute is set. In 
the following examples, we will refer to this as a setter.
• __get__(self, obj, owner=None): This is called whenever the attribute is 
read (referred to as a getter).
• __delete__(self, obj): This is called when del is invoked on the attribute.

 

A descriptor that implements __get__() and __set__() is called a data descriptor. If it just implements __get__(), then it is called a non-data descriptor.

Methods of the descriptor protocol are, in fact, called by the object's special __getattribute__() method on every attribute lookup.

1. It verifies whether the attribute is a data descriptor on the class object of the 
instance
2. If not, it looks to see whether the attribute can be found in the __dict__
lookup of the instance object
3. Finally, it looks to see whether the attribute is a non-data descriptor on the 
class object of the instance

 

 Python already uses the descriptor protocol to bind class functions to instances as methods. 
Descriptors also power the mechanism behind the classmethod and staticmethod decorators. 

Monkey patching is the technique of modifying the class instance dynamically at runtime by adding, modifying, or deleting attributes without touching the class definition or the source code.
 


Responses(0)







Related