setattr 讲解
1 | setattr( object, name, value) |
setattr 实践
代码来源
首先,去掉 第15行的注释运行,结果显示如下
1
2
3
4
5
6$ python py_setattr.py
attr4 exists?
Traceback (most recent call last):
File "py_setattr.py", line 15, in <module>
print T.attr4
AttributeError: 'test' object has no attribute 'attr4'
再加上注释,运行
1
2
3
4
5$ python py_setattr.py
attr4 exists?
create new attribute <attr4> test4
attribute <attr3>:test3
changed attribute <attr3> :test3_changed