variable variables

someone petshmidt at googlemail.com
Fri Jun 18 06:31:06 EDT 2010


On Jun 18, 12:01 pm, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
wrote:
> En Fri, 18 Jun 2010 06:48:34 -0300, someone <petshm... at 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 :)

>
> --
> Gabriel Genellina




More information about the Python-list mailing list