Setting a class attribute given its name

Edward C. Jones edcjones at
Thu Jun 19 05:28:55 CEST 2003

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)

Ed Jones

More information about the Python-list mailing list