Question: Evaluate an string variable's value to a variable

Mir Nazim mirnazim at gmail.com
Sun Dec 21 08:05:35 CET 2008


On Dec 21, 11:56 am, "Chris Rebert" <c... at rebertia.com> wrote:
> On Sat, Dec 20, 2008 at 10:49 PM, Mir Nazim <mirna... at gmail.com> wrote:
> > Just a quick question.
>
> > For example I have
>
> >>>> class X
> >>>> ....pass
>
> > Then I do
> >>>> x = X()
> >>>> x.name = 'Nazim
>
> > Now my question is whether something like below is possible and how
> >>>> y = 'name'
> >>>> print x.y   #  How can x.y can be evaluated to x.name
>
> print getattr(x, y) #==> Nazim
>
> There are also analogous setattr() and delattr() functions.
>

Damn Right.
These functions simply eluded my mind for well 15 minutes

Thanks a lot Chris.

> Cheers,
> Chris
>
> --
> Follow the path of the Iguana...http://rebertia.com




More information about the Python-list mailing list