Descriptor: class name precedence over instance name

Ian Kelly ian.g.kelly at gmail.com
Sat Jul 2 00:51:08 EDT 2016


On Fri, Jul 1, 2016 at 10:27 PM, Veek. M <vek.m1234 at gmail.com> wrote:
> Trying to make sense of this para:
>
> --------------------------------------------------
> Also, the attribute name used by the class to hold a descriptor takes
> prece- dence over attributes stored on instances.
>
> In the previous example,
> this is why the descriptor object takes a name parameter and why the
> name is changed slightly by inserting a leading underscore. In order
> for the descriptor to store a value on the instance, it has to pick a
> name that is different than that being used by the descriptor itself
> ---------------------------------------------
> Under normal circumstances, when I do an attribute lookup:
> x = Foo()
> x.a
> he will first check 'x' then Foo.
>
> Is he say that Descriptors are a special case where Foo is checked
> first,

It depends whether it's a "data descriptor" or not. A data descriptor
is one that defines at least one of __set__ or __delete__, not just
__get__. Data descriptors take precendence over instance attributes.
Instance attributes take precedence over non-data descriptors.

> then what - Base classes..?

Checking base classes is part of checking the class. If a base class
has a data descriptor, that will likewise take precedence over the
instance attribute, which will likewise take precedence over non-data
descriptors in the base class.

> or does he hop back to look in the
> instance? How is C3 Linearization altered?

It's not.

> Additionally,
> class Foo:
>    def __init__(self, name, value):
>      self.name = name
>
> cannot be done because
> x = Foo('bar', 10)
>
> x.bar will..? attribute in class takes precedence.. great, isn't that
> what we want?

I don't understand what you're asking here or what this example has to
do with descriptors.



More information about the Python-list mailing list