I was kinda joking with the function. More pythonically:
def getattrchain(obj, attlist):
for item in attlist:
obj = getattr(obj, item)
return obj
-- Wade
> -----Original Message-----
> From: Wade Leftwich [mailto:[EMAIL PROTECTED]]
> Sent: Monday, July 23, 2001 6:00 PM
> To: Lloyd Kvam; Active Python
> Subject: RE: Using getattr() with "dotted" names
>
>
> >>> getattr(a.b, 'prop')
> 1234
> >>> getattr(getattr(a,'b'), 'prop')
> 1234
> >>>
> >>> def getattrchain(obj, attlist):
> ... if not attlist:
> ... return obj
> ... else:
> ... return getattrchain(getattr(obj, attlist[0]), attlist[1:])
> ...
> >>> getattrchain(a, ('b', 'prop'))
> 1234
>
>
> -- Wade Leftwich
> Ithaca, NY
>
>
> > -----Original Message-----
> > From: [EMAIL PROTECTED]
> > [mailto:[EMAIL PROTECTED]]On Behalf Of Lloyd
> > Kvam
> > Sent: Monday, July 23, 2001 4:51 PM
> > To: Active Python
> > Subject: Using getattr() with "dotted" names
> >
> >
> > I have a project where classes may be nested. The Python
> > interpreter will handle nested instance references without any
> > difficulty (e.g. print a.b.prop).
> >
> > Is there a good way to handle attribute references in getattr to
> > get the same effect?
> > (e.g. print getattr( a, 'b.prop') )
> >
> > class A:
> > pass
> > class B:
> > pass
> > >>>>>>>>>
> > a = A()
> > a.b = B()
> > a.b.prop = 1234
> > print a.b.prop
> > <<<<<<<<< All work OK
> >
> > print getattr( a, 'b.prop') does not work.
> >
> >
> > --
> > Lloyd Kvam
> > Venix Corp.
> > 1 Court Street, Suite 378
> > Lebanon, NH 03766-1358
> >
> > voice: 603-443-6155
> > fax: 801-459-9582
> > _______________________________________________
> > ActivePython mailing list
> > [EMAIL PROTECTED]
> > http://listserv.ActiveState.com/mailman/listinfo/activepython
> >
_______________________________________________
ActivePython mailing list
[EMAIL PROTECTED]
http://listserv.ActiveState.com/mailman/listinfo/activepython