#object生成的对象不能使用setattr函数
>>> o=object() >>> setattr(o,'name','ok') Traceback (most recent call last): File "<pyshell#3>", line 1, in <module> setattr(o,'name','ok') AttributeError: 'object' object has no attribute 'name'
#类对象生成的实例,可以使用setattr >>> class test(object): name="xiaohua" def run(self): return "HelloWord" >>> t=test() >>> setattr(t, "age", "18")>>> print hasattr(t, "age") True >>>