[ubuntu-in] OT: Evaluate an string variable's value to a variable in Python

Aditya M aditya87 at gmail.com
Sun Dec 21 07:15:30 GMT 2008


On Sun, Dec 21, 2008 at 12:21, Mir Nazim <mirnazim at gmail.com> wrote:

> Hi guys, sorry for the off topic posting, but I know there are a
> couple of python guys on the list, so could not resist asking here
> 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
>

You can use:

print x.__dict__[y]

It will have the same result as:

print x.name


-- 
Aditya Manthramurthy
-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://lists.ubuntu.com/archives/ubuntu-in/attachments/20081221/088a6e4b/attachment.htm 


More information about the ubuntu-in mailing list