extend getattr()

Discussion in 'Python' started by Rotlaus, Jun 26, 2008.

  1. Rotlaus

    Rotlaus Guest

    Hello,

    lets assume i have some classes:

    class A(object):
    def __init__(self):
    b = B()

    class B(object):
    def __init__(self):
    c = C()

    class C(object):
    def __init__(self):
    pass

    and now i wanna do something like this:

    a=A()
    c=getattr(a, 'b.c')

    I know this doesn't work, but what can i do to get this or a similar
    functionality to get it work for this sample and for even more nested
    classes?

    Kind regards,

    Andre
    Rotlaus, Jun 26, 2008
    #1
    1. Advertising

  2. Rotlaus wrote:
    > Hello,
    >
    > lets assume i have some classes:
    > [...]
    >
    > a=A()
    > c=getattr(a, 'b.c')
    >
    > I know this doesn't work, but what can i do to get this or a similar
    > functionality to get it work for this sample and for even more nested
    > classes?


    Just recursively apply the getattr(), like this:

    class A(object):
    def __init__(self):
    self.b = B()

    class B(object):
    def __init__(self):
    self.c = C()

    class C(object):
    def __init__(self):
    pass

    def ext_getattr(obj, attr):
    for subattr in attr.split("."):
    obj = getattr(obj, subattr)
    return obj

    a=A()
    c = ext_getattr(a, 'b.c')

    -- Gerhard
    Gerhard Häring, Jun 26, 2008
    #2
    1. Advertising

  3. Le Thursday 26 June 2008 13:06:53 Rotlaus, vous avez écrit :
    > Hello,
    >
    > lets assume i have some classes:
    >
    > class A(object):
    > def __init__(self):
    > b = B()
    >
    > class B(object):
    > def __init__(self):
    > c = C()
    >


    note you're just defining some local variables here, should be self.b = B()
    and self.c = C().

    > class C(object):
    > def __init__(self):
    > pass
    >
    > and now i wanna do something like this:
    >
    > a=A()
    > c=getattr(a, 'b.c')
    >
    > I know this doesn't work, but what can i do to get this or a similar
    > functionality to get it work for this sample and for even more nested
    > classes?
    >


    You could do it manually:

    c = getattr(getattr(a, 'b'), 'c')

    or make it automatic:

    def get_dotted_attr (obj, dotted_attr) :
    for attr in dotted_attr.split('.') :
    obj = getattr(obj, attr)
    return obj

    a = A()
    print 'a.b.c = %s' % get_dotted_attr(a, 'b.c')

    --
    Cédric Lucantis
    Cédric Lucantis, Jun 26, 2008
    #3
  4. On Jun 26, 7:39 am, Cédric Lucantis <> wrote:

    > Le Thursday 26 June 2008 13:06:53 Rotlaus, vous avez écrit :
    >
    > > Hello,

    >
    > > lets assume i have some classes:

    >
    > > class A(object):
    > > def __init__(self):
    > > b = B()

    >
    > > class B(object):
    > > def __init__(self):
    > > c = C()

    >
    > note you're just defining some local variables here, should be self.b = B()
    > and self.c = C().
    >
    > > class C(object):
    > > def __init__(self):
    > > pass

    >
    > > and now i wanna do something like this:

    >
    > > a=A()
    > > c=getattr(a, 'b.c')

    >
    > > I know this doesn't work, but what can i do to get this or a similar
    > > functionality to get it work for this sample and for even more nested
    > > classes?

    >
    > You could do it manually:
    >
    > c = getattr(getattr(a, 'b'), 'c')
    >
    > or make it automatic:
    >
    > def get_dotted_attr (obj, dotted_attr) :
    > for attr in dotted_attr.split('.') :
    > obj = getattr(obj, attr)
    > return obj
    >
    > a = A()
    > print 'a.b.c = %s' % get_dotted_attr(a, 'b.c')


    FYI, this feature will exist in operator.attrgetter from Python 2.6,
    i.e. you'll be able to say attrgetter('b.c')(a).

    George
    George Sakkis, Jun 26, 2008
    #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. daishi
    Replies:
    0
    Views:
    548
    daishi
    Jul 25, 2003
  2. Srikanth Mandava

    getattr

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

    Confused about hasattr/getattr/namespaces

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

    getattr() in default namespace.

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

    getattr() woes

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

Share This Page