__set_name__(self, owner, name)

When we create the descriptor object in the class that is going to use it, we generally need the descriptor to know the name of the attribute it is going to be handling.

This attribute name is the one we use to read from and write to __dict__ in the __get__ and __set__ methods, respectively.

Before Python 3.6, the descriptor couldn't take this name automatically, so the most general approach was to just pass it explicitly when initializing the object. This works fine, but it has an issue in that it requires that we duplicate the name every time we want to use the descriptor for a new attribute.

This is what a typical descriptor would look like if we didn't have this method:

class DescriptorWithName:
def __init__(self, name):
self.name = name

def __get__(self, instance, value):
if instance is None:
return self
logger.info("getting %r attribute from %r", self.name, instance)
return instance.__dict__[self.name]

def __set__(self, instance, value):
instance.__dict__[self.name] = value


class ClientClass:
descriptor = DescriptorWithName("descriptor")

We can see how the descriptor uses this value:

>>> client = ClientClass()
>>> client.descriptor = "value"
>>> client.descriptor
INFO:getting 'descriptor' attribute from <ClientClass object at 0x...>
'value'

Now, if we wanted to avoid writing the name of the attribute twice (once for the variable assigned inside the class, and once again as the name of the first parameter of the descriptor), we have to resort to a few tricks, like using a class decorator, or (even worse) using a metaclass.

In Python 3.6, the new method __set_name__ was added, and it receives the class where that descriptor is being created, and the name that is being given to that descriptor. The most common idiom is to use this method for the descriptor so that it can store the required name in this method.

For compatibility, it is generally a good idea to keep a default value in the __init__ method but still take advantage of __set_name__.

With this method, we can rewrite the previous descriptors as follows:

class DescriptorWithName:
def __init__(self, name=None):
self.name = name

def __set_name__(self, owner, name):
self.name = name
...
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.201.93