这有一个内置函数:
setattr(test, attr_name, 10)
参考:http://docs.python.org/library/functions.html#setattr
例:
>>> class a(object): pass >>> a.__dict__['wut'] = 4 Traceback (most recent call last): File "", line 1, in TypeError: 'dictproxy' object does not support item assignment >>> setattr(a, 'wut', 7) >>> a.wut 7