On 06/18/10 20:31, someone wrote: > On Jun 18, 12:01 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar> > wrote: >> En Fri, 18 Jun 2010 06:48:34 -0300, someone <petshm...@googlemail.com> >> escribió: >> >>> is it possible to make first attr variable? >> >>> some_object.attr.attr >> >>> so instead of attr I could use self.foo which has value "attr" >> >> I think you're looking for >> getattr:http://docs.python.org/library/functions.html#getattr >> >> name = "spam" >> getattr(some_object, name) == some_object.spam == getattr(some_object, >> "spam") > > Thanks. > > I was looking for a "short way" to do it because I have a lot > "some_object.attr.attr or some_object.other_attr.attr" in code. it > looks like I cannot replace attr with just other variable and must > type some_object.other_attr.attr or your solution which is however > longer to type :)
Alternatively, if you have control over some_object class: class SomeClass(object): def __init__(self): pass def __getattr__(self, attr): return "attr: " + attr some_object = SomeClass() print some_object.foo print some_object.myattr -- http://mail.python.org/mailman/listinfo/python-list