use str as variable name

Mathieu Prevot mathieu.prevot at gmail.com
Thu Sep 4 03:59:10 EDT 2008


2008/9/4 Chris Rebert <cvrebert at gmail.com>:
> On Thu, Sep 4, 2008 at 12:25 AM, Mathieu Prevot
> <mathieu.prevot at gmail.com> wrote:
>> Hi,
>>
>> I have a program that take a word as argument, and I would like to
>> link this word to a class variable.
>>
>> eg.
>> class foo():
>
> You should subclass 'object', so that should be:
>    class Foo(object):
>
>>  width = 10
>>  height = 20
>>
>> a=foo()
>> arg='height'
>> a.__argname__= new_value
>
> You're looking for the setattr() built-in function. In this exact case:
>    setattr(a, arg, new_value)
>
> This is probably covered in the Python tutorial, please read it.
>
> Regards,
> Chris

Indeed.

I'll use:
a.__setattr__(height, new_value)

Thanks to all
Mathieu



More information about the Python-list mailing list