我想知道为什么能够为自定义类型的实例创建一个新属性(“新”表示“以前未在类主体中定义”),但不能为内置的-在类型上,就像object
它本身一样。
代码示例:
>>> class SomeClass(object):
... pass
...
>>> sc = SomeClass()
>>> sc.name = "AAA"
>>> sc.name
'AAA'
>>> obj = object()
>>> obj.name = "BBB"
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'object' object has no attribute 'name'