extended setattr()

Discussion in 'Python' started by Rotlaus, Jul 7, 2008.

  1. Rotlaus

    Rotlaus Guest

    2 weeks ago i asked for a etended getattr() which worked really fine,
    but now i would love to have a extended setattr() as well.

    Lets assume i have some classes:

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

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

    class C(object):
    def __init__(self, foo='', bar=''):
    self.foo = foo
    self.bar = bar

    and now i wanna do something like this:

    a=A()
    ext_setattr(a, 'B.C', ('a', 'b'))

    Is this possible? It would also be nice if the attributes would be
    created if they not exist, always implying that
    objectname==objecttype.

    Kind regards,

    Andre
     
    Rotlaus, Jul 7, 2008
    #1
    1. Advertising

  2. Rotlaus

    Rotlaus Guest

    On 7 Jul., 08:01, Rotlaus <> wrote:
    > 2 weeks ago i asked for a etended getattr() which worked really fine,
    > but now i would love to have a extendedsetattr() as well.


    I've tried the following, but it doesn't work:

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

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

    class C(object):
    def __init__(self, txt=''):
    self.txt = txt

    def ext_setattr(obj, attr, val):
    for subattr in attr.split("."):
    obj = getattr(obj, subattr)
    obj = val

    >>> import test
    >>> a = A()

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    NameError: name 'A' is not defined
    >>> a = test.A()
    >>> a.B.C.txt

    'foo'
    >>> ext_setattr(a, 'B.C.txt', 'bar')
    >>> a.B.C.txt

    'foo'

    What am i doing wrong?
     
    Rotlaus, Jul 8, 2008
    #2
    1. Advertising

  3. Rotlaus schrieb:
    > On 7 Jul., 08:01, Rotlaus <> wrote:
    >> 2 weeks ago i asked for a etended getattr() which worked really fine,
    >> but now i would love to have a extendedsetattr() as well.

    >
    > I've tried the following, but it doesn't work:
    >
    > class A(object):
    > def __init__(self):
    > self.B = B()
    >
    > class B(object):
    > def __init__(self):
    > self.C = C('foo')
    >
    > class C(object):
    > def __init__(self, txt=''):
    > self.txt = txt
    >
    > def ext_setattr(obj, attr, val):
    > for subattr in attr.split("."):
    > obj = getattr(obj, subattr)
    > obj = val
    >
    >>>> import test
    >>>> a = A()

    > Traceback (most recent call last):
    > File "<stdin>", line 1, in <module>
    > NameError: name 'A' is not defined
    >>>> a = test.A()
    >>>> a.B.C.txt

    > 'foo'
    >>>> ext_setattr(a, 'B.C.txt', 'bar')
    >>>> a.B.C.txt

    > 'foo'
    >
    > What am i doing wrong?


    obj = val won't work.

    You need to use a setattr(obj, name, val)

    on the last attribute-name.

    Diez
     
    Diez B. Roggisch, Jul 8, 2008
    #3
  4. Rotlaus

    Andre Adrian Guest

    Diez B. Roggisch <deets <at> nospam.web.de> writes:

    > > def ext_setattr(obj, attr, val):
    > > for subattr in attr.split("."):
    > > obj = getattr(obj, subattr)
    > > obj = val
    > >
    > >>>> import test
    > >>>> a = A()

    > > Traceback (most recent call last):
    > > File "<stdin>", line 1, in <module>
    > > NameError: name 'A' is not defined
    > >>>> a = test.A()
    > >>>> a.B.C.txt

    > > 'foo'
    > >>>> ext_setattr(a, 'B.C.txt', 'bar')
    > >>>> a.B.C.txt

    > > 'foo'
    > >
    > > What am i doing wrong?

    >
    > obj = val won't work.


    Why is this so? Shouldn't it be the same?

    > You need to use a setattr(obj, name, val)
    > on the last attribute-name.


    Ok, so this works:

    def ext_setattr(obj, attr, val):
    attributes = attr.split('.')
    for subattr in attributes[:-1]:
    obj = getattr(obj, subattr)
    setattr(obj, attributes[-1], val)
     
    Andre Adrian, Jul 8, 2008
    #4
  5. Andre Adrian wrote:

    > Diez B. Roggisch <deets <at> nospam.web.de> writes:
    >
    >> > def ext_setattr(obj, attr, val):
    >> > for subattr in attr.split("."):
    >> > obj = getattr(obj, subattr)
    >> > obj = val
    >> >
    >> >>>> import test
    >> >>>> a = A()
    >> > Traceback (most recent call last):
    >> > File "<stdin>", line 1, in <module>
    >> > NameError: name 'A' is not defined
    >> >>>> a = test.A()
    >> >>>> a.B.C.txt
    >> > 'foo'
    >> >>>> ext_setattr(a, 'B.C.txt', 'bar')
    >> >>>> a.B.C.txt
    >> > 'foo'
    >> >
    >> > What am i doing wrong?

    >>
    >> obj = val won't work.

    >
    > Why is this so? Shouldn't it be the same?


    No, of course not!

    obj = val

    binds the object reffered to by val to the LOCAL name obj. That's python
    101, make sure you get variables/names and scopes proper.

    >> You need to use a setattr(obj, name, val)
    >> on the last attribute-name.

    >
    > Ok, so this works:
    >
    > def ext_setattr(obj, attr, val):
    > attributes = attr.split('.')
    > for subattr in attributes[:-1]:
    > obj = getattr(obj, subattr)
    > setattr(obj, attributes[-1], val)


    Yep.

    Diez
     
    Diez B. Roggisch, Jul 8, 2008
    #5
    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. Roman Yakovenko

    __slots__, setattr, __metaclass__

    Roman Yakovenko, Jan 12, 2004, in forum: Python
    Replies:
    1
    Views:
    304
    Michele Simionato
    Jan 12, 2004
  2. Gerson Kurz
    Replies:
    4
    Views:
    396
    Larry Bates
    Jun 14, 2004
  3. dataangel

    Using pickle with setattr

    dataangel, Oct 13, 2004, in forum: Python
    Replies:
    0
    Views:
    286
    dataangel
    Oct 13, 2004
  4. dataangel

    Re: Using pickle with setattr

    dataangel, Oct 14, 2004, in forum: Python
    Replies:
    3
    Views:
    324
    dataangel
    Oct 15, 2004
  5. kramb64

    setattr inside a module

    kramb64, Mar 23, 2005, in forum: Python
    Replies:
    5
    Views:
    288
    Jeremy Bowers
    Mar 23, 2005
Loading...

Share This Page