10
>>> a = object()
>>> a.x = 5
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'object' object has no attribute 'x'
>>> b = lambda:0
>>> b.x = 5
>>> b.x
5

Why do instances of the object class not have a __dict__, causing it to behave as semantically immutable? What were the reasons for choosing this design?

Specifically, why:

instances of types defined in C don't have a __dict__ attribute by default.

As noted in this question.

Community
  • 1
  • 1
Sean Pianka
  • 2,157
  • 2
  • 27
  • 43
  • Possible duplicate of [Python: why is \_\_dict\_\_ attribute not in built-in class instances](http://stackoverflow.com/questions/9502183/python-why-is-dict-attribute-not-in-built-in-class-instances) – juanpa.arrivillaga Nov 28 '16 at 21:20

1 Answers1

7

The documentation for Python 2 is not very helpful in giving an explanation as to why you cannot assign attributes to an object(), but the documentation for Python 3 provides a bit more information:

Return a new featureless object. object is a base for all classes. It has the methods that are common to all instances of Python classes. This function does not accept any arguments.

Note: object does not have a __dict__, so you can’t assign arbitrary attributes to an instance of the object class.

Thus, the reason you cannot add arbitrary attributes to your object() appears to be because of the fact that object() instances do not have an implementation of the __dict__ attribute, not because object() instances are immutable:

>>> hasattr(object(), '__dict__')
False
>>>

Another interesting thing, but perhaps not relevant to the discussion at hand, is that while an instance of object may not have a __dict__ implementation, the object class itself does:

>>> hasattr(object, '__dict__')
True

As for the why part of the question, I cannot find any exact reasons for why object() doesn't have a __dict__. Is is probably because - as @tdelany has already mentioned on in the comments - an implementation detail. If you really want a definitive answer, you should ask Guido himself.

Community
  • 1
  • 1
Christian Dean
  • 22,138
  • 7
  • 54
  • 87
  • @SeanPianka No. But I'm still looking. I thought it would be useful to show why you cannot add attributes to an `object()`. – Christian Dean Nov 28 '16 at 21:19
  • Because it is a builtin class. – juanpa.arrivillaga Nov 28 '16 at 21:20
  • 3
    `object` is implemented in C and its common that extension objects don't take extra attributes. Considering that regular class instance `__dict__` is an object, its not surprising the `object` doesnt have one! – tdelaney Nov 28 '16 at 21:21
  • @SeanPianka Like I said, I'm still workin' on it. Gimme some time :) – Christian Dean Nov 28 '16 at 21:22
  • I think the answer is good... the docs say you can't do it and the reason is an implementation detail. You may find that it does work for other implementations. – tdelaney Nov 28 '16 at 21:23
  • @tdelaney Well I'll do a quick search anyways. – Christian Dean Nov 28 '16 at 21:23
  • 2
    @leaf - here's a good place: https://hg.python.org/cpython/file/tip/Include/object.h. It will show you the details of the implementation. – tdelaney Nov 28 '16 at 21:26
  • @Jean-FrançoisFabre Thanks. But I sort of wish I could have come up with a logical explanation as to why `object()` doesn't have a `__dict__` method. But I guess calling on Guido is the logical :) – Christian Dean Nov 29 '16 at 23:35