extend getattr()

2008-06-26 Thread Rotlaus
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
--
http://mail.python.org/mailman/listinfo/python-list


extended setattr()

2008-07-06 Thread Rotlaus
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
--
http://mail.python.org/mailman/listinfo/python-list


Re: extended setattr()

2008-07-07 Thread Rotlaus
On 7 Jul., 08:01, Rotlaus <[EMAIL PROTECTED]> 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 "", line 1, in 
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?
--
http://mail.python.org/mailman/listinfo/python-list