getattr

Discussion in 'Python' started by kyaBey@gmail.com, Sep 30, 2005.

  1. Guest

    This question is regarding the __getattr__ function defined for every
    object.
    Consider the following example

    Assume that foo is an instance of class Foo, and the following are
    references to foo's field "bar" which is an instance of class Bar

    a) foo.bar
    b) foo.bar.spam - spam is a member of "bar"

    I want the above references to be handled by __getattr__. Hence I do
    not have an entry for the 'bar' in foo.__dict__

    Is there any way by which the __getattr__(self,attr) method can
    determine that in
    case a) attr == 'bar' is the final component in the reference unlike in
    case b) where attr=='bar' is NOT the ultimate(final) component of
    reference and is an intermediate component in the reference.

    tia
    , Sep 30, 2005
    #1
    1. Advertising

  2. wrote:

    > Is there any way by which the __getattr__(self,attr) method can
    > determine that in
    > case a) attr == 'bar' is the final component in the reference unlike in
    > case b) where attr=='bar' is NOT the ultimate(final) component of
    > reference and is an intermediate component in the reference.


    no.

    if you want to control further accesses, your __getattr__ has to return a
    proxy object, and use a suitable syntax to get the final value.

    </F>
    Fredrik Lundh, Sep 30, 2005
    #2
    1. Advertising


  3. > if you want to control further accesses, your __getattr__ has to return a
    > proxy object, and use a suitable syntax to get the final value.


    message.insert(index, "your users have to ")

    </F>
    Fredrik Lundh, Sep 30, 2005
    #3
    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. daishi
    Replies:
    0
    Views:
    537
    daishi
    Jul 25, 2003
  2. Srikanth Mandava

    getattr

    Srikanth Mandava, Feb 19, 2004, in forum: Python
    Replies:
    2
    Views:
    439
    Peter Hansen
    Feb 19, 2004
  3. Brian Roberts

    Confused about hasattr/getattr/namespaces

    Brian Roberts, Feb 29, 2004, in forum: Python
    Replies:
    2
    Views:
    294
    Bob Ippolito
    Feb 29, 2004
  4. SimonVC

    getattr() in default namespace.

    SimonVC, Apr 8, 2004, in forum: Python
    Replies:
    3
    Views:
    868
    SimonVC
    Apr 9, 2004
  5. Thomas Rast

    getattr() woes

    Thomas Rast, Dec 29, 2004, in forum: Python
    Replies:
    4
    Views:
    615
    Kamilche
    Dec 31, 2004
Loading...

Share This Page