In Python, I can add an attribute to a class C which I have previously defined. However, I cannot add an attribute to list - the resulting error message explains that this is because list is a built-in type:
>>> class C: pass
...
>>> C.foo = 1
>>> C.foo
1
>>> list.foo = 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't set attributes of built-in/extension type 'list'
Similarly, it is possible to add an attribute to an instance of C, but not to an instance of list. In this case, however, the error message is much more vague:
>>> o = C()
>>> o.bar = 2
>>> o.bar
2
>>> o = []
>>> o.bar = 2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'bar'
Why can't I add members to instances of list? Is it, again, because list is a built-in type?
More generally, which objects in Python can have attributes added dynamically?