Setting a class attribute given its name
Andrew Bennetts
andrew-pythonlist at puzzling.org
Wed Jun 18 23:44:58 EDT 2003
On Wed, Jun 18, 2003 at 11:28:55PM -0400, Edward C. Jones wrote:
> The standard way to set a class attribute is
>
> class X(object):
> i = 1
>
> Given the string 'i', how do I set the class attribute? The following
> doesn't work (Python 2.2.3):
>
> class X(object):
> setattr(X, 'i', 1)
That doesn't work because the name 'X' isn't bound yet. This will work,
though:
class X(object):
pass
setattr(X, 'i', 1)
-Andrew.
More information about the Python-list
mailing list