Setting a class attribute given its name

Edward C. Jones edcjones at erols.com
Wed Jun 18 23:28:55 EDT 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)

Thanks,
Ed Jones





More information about the Python-list mailing list