instance.attribute lookup

Discussion in 'Python' started by Ethan Furman, Oct 5, 2012.

  1. Ethan Furman

    Ethan Furman Guest

    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
     
    Ethan Furman, Oct 5, 2012
    #1
    1. Advertising

  2. 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
     
    Steven D'Aprano, Oct 6, 2012
    #2
    1. Advertising

  3. Ethan Furman

    Ethan Furman Guest

    Steven D'Aprano wrote:
    > 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?


    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~
     
    Ethan Furman, Oct 6, 2012
    #3
  4. On 06/10/2012 00:12, Ethan Furman wrote:
    > Steven D'Aprano wrote:
    >> 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?

    >
    > 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.
     
    Mark Lawrence, Oct 6, 2012
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Donnal Walter

    class attribute to instance attribute

    Donnal Walter, Jun 30, 2005, in forum: Python
    Replies:
    4
    Views:
    476
    Greg Ewing
    Jul 6, 2005
  2. Kent Johnson
    Replies:
    5
    Views:
    278
    Kent Johnson
    Nov 14, 2005
  3. Russell Warren
    Replies:
    5
    Views:
    478
    Russell Warren
    Jan 17, 2006
  4. 陈伟
    Replies:
    9
    Views:
    264
    Hans Mulder
    Aug 30, 2012
  5. Ian Kelly

    Re: instance.attribute lookup

    Ian Kelly, Oct 6, 2012, in forum: Python
    Replies:
    0
    Views:
    167
    Ian Kelly
    Oct 6, 2012
Loading...

Share This Page