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

Mir Nazim mirnazim at gmail.com
Sun Dec 21 07:20:00 GMT 2008


Thanks Aditya.

Mir Nazim
Cell: +91 9469071855
Blog: http://saunzal.org
Company:http://www.ikraftsoft.com




On Sun, Dec 21, 2008 at 12:45 PM, Aditya M <aditya87 at gmail.com> wrote:
> 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
>
>
> --
> ubuntu-in mailing list
> ubuntu-in at lists.ubuntu.com
> https://lists.ubuntu.com/mailman/listinfo/ubuntu-in
>
>



More information about the ubuntu-in mailing list