0
为什么在Python中为每个类实例动态定义一个新的属性是可能的,只要它不是object
类型?在Python中动态定义新的实例属性
class NewStyle(object):
pass
class OldStyle:
pass
a = object()
# This line raises AttributeError
a.foo = 1
# All of these work fine
a = Exception()
a.foo = 1
a = OldStyle()
a.foo = 1
a = NewStyle()
a.foo = 1
这种行为似乎是相同的在Python 3
哪里的默认实现__dict__,来自如果不从'object'? – reish 2014-09-03 13:48:05