Understanding descriptors
Brian Allen Vanderburg II
BrianVanderburg2 at aim.com
Thu Feb 5 07:04:40 EST 2009
bruno.42.desthuilliers at websiteburo.invalid wrote:
>
> So the lookup chain is:
>
> 1/ lookup the class and bases for a binding descriptor
> 2/ then lookup the instance's __dict__
> 3/ then lookup the class and bases for a non-binding descriptor or
> plain attribute
> 4/ then class __getattr__
>
> Also and FWIW, there's a "step zero" : calls __getattribute__. All the
> above lookup mechanism is actually implemented by
> object.__getattribute__.
Okay, so instance attributes never use their __get__/__set__/etc when
looking up.
A binding descriptor is one that has a __set__ (even if it doesn't do
anything) and it takes priority over instance variables. Properties are
binding descriptors even if they don't have a set function specified. A
non-binding descriptor doesn't have __set__ and instance variables take
priority over them.
For reading:
1. Lookup in the class/bases for a binding descriptor and if found use
its __get__
2. If instance, look up in instance __dict__ and if found return it
3. Lookup in the class/bases
a. if found and a descriptor use it's __get__
b. if found and not a descriptor return it
4. Use __getattr__ (if instance?)
For writing:
1. If instance
a. lookup in the class/bases for a binding descriptor and if found
use its __set__
b. write to instance __dict__
2. If class, write in class __dict__
I think I understand it now. Thanks.
Brian Vanderburg II
More information about the Python-list
mailing list