Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

instance.attribute lookup

23 views
Skip to first unread message

Ethan Furman

unread,
Oct 5, 2012, 1:39:53 PM10/5/12
to Python
There is a StackOverflow question [1] that points to this on-line book
[2] which has a five-step sequence for looking up attributes:

> When retrieving an attribute from an object (print
> objectname.attrname) Python follows these steps:
>
> 1. If attrname is a special (i.e. Python-provided) attribute for
> objectname, return it.
>
> 2. Check objectname.__class__.__dict__ for attrname. If it exists and
> is a data-descriptor, return the descriptor result. Search all bases
> of objectname.__class__ for the same case.
>
> 3. Check objectname.__dict__ for attrname, and return if found. If
> objectname is a class, search its bases too. If it is a class and a
> descriptor exists in it or its bases, return the descriptor result.
>
> 4. Check objectname.__class__.__dict__ for attrname. If it exists and
> is a non-data descriptor, return the descriptor result. If it exists,
> and is not a descriptor, just return it. If it exists and is a data
> descriptor, we shouldn't be here because we would have returned at
> point 2. Search all bases of objectname.__class__ for same case.
>
> 5. Raise AttributeError

I'm thinking step 1 is flat-out wrong and doesn't exist. Does anybody
know otherwise?

~Ethan~

[1]
http://stackoverflow.com/q/10536539/208880

[2]
http://www.cafepy.com/article/python_attributes_and_methods/ch01s05.html

Steven D'Aprano

unread,
Oct 5, 2012, 7:00:30 PM10/5/12
to
On Fri, 05 Oct 2012 10:39:53 -0700, Ethan Furman wrote:

> There is a StackOverflow question [1] that points to this on-line book
> [2] which has a five-step sequence for looking up attributes:
>
> > When retrieving an attribute from an object (print
> > objectname.attrname) Python follows these steps:
> >
> > 1. If attrname is a special (i.e. Python-provided) attribute for
> > objectname, return it.
[...]
> I'm thinking step 1 is flat-out wrong and doesn't exist. Does anybody
> know otherwise?

I'm thinking I don't even understand what step 1 means.

What's a Python-provided attribute, and how is it different from other
attributes?



--
Steven

Ethan Furman

unread,
Oct 5, 2012, 7:12:22 PM10/5/12
to pytho...@python.org
Well, if /you/ don't understand it I feel a lot better about not
understanding it either! :)

Glad to know I'm not missing something (besides ESP, a crystal ball, and
a mind-reader!)

~Ethan~

Christian Heimes

unread,
Oct 5, 2012, 7:26:32 PM10/5/12
to pytho...@python.org
Am 05.10.2012 19:39, schrieb Ethan Furman:
> I'm thinking step 1 is flat-out wrong and doesn't exist. Does anybody
> know otherwise?

The answer is confusing and also wrong. For instance it ignores the
existence of __slots__, metaclasses and the different lookup strategy of
__special__ methods in old style and new style classes.

Ian Kelly

unread,
Oct 5, 2012, 7:29:51 PM10/5/12
to Python
On Fri, Oct 5, 2012 at 11:39 AM, Ethan Furman <et...@stoneleaf.us> wrote:
> There is a StackOverflow question [1] that points to this on-line book [2]
> which has a five-step sequence for looking up attributes:
>
>> When retrieving an attribute from an object (print
>> objectname.attrname) Python follows these steps:
>>
>> 1. If attrname is a special (i.e. Python-provided) attribute for
>> objectname, return it.
>>
>> 2. Check objectname.__class__.__dict__ for attrname. If it exists and
>> is a data-descriptor, return the descriptor result. Search all bases
>> of objectname.__class__ for the same case.
>>
>> 3. Check objectname.__dict__ for attrname, and return if found. If
>> objectname is a class, search its bases too. If it is a class and a
>> descriptor exists in it or its bases, return the descriptor result.
>>
>> 4. Check objectname.__class__.__dict__ for attrname. If it exists and
>> is a non-data descriptor, return the descriptor result. If it exists,
>> and is not a descriptor, just return it. If it exists and is a data
>> descriptor, we shouldn't be here because we would have returned at
>> point 2. Search all bases of objectname.__class__ for same case.
>>
>> 5. Raise AttributeError
>
> I'm thinking step 1 is flat-out wrong and doesn't exist. Does anybody know
> otherwise?

I think step 1 refers to looking up attributes like "foo.__class__" or
"foo.__dict__" themselves.

Mark Lawrence

unread,
Oct 5, 2012, 7:32:44 PM10/5/12
to pytho...@python.org
On 06/10/2012 00:12, Ethan Furman wrote:
> Well, if /you/ don't understand it I feel a lot better about not
> understanding it either! :)
>
> Glad to know I'm not missing something (besides ESP, a crystal ball, and
> a mind-reader!)
>
> ~Ethan~

My probably highly uneducated guess is that "Python-provided attribute"
refers to double underscore names. YMMV by several trillion light years :)

--
Cheers.

Mark Lawrence.

0 new messages