This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Attribute assignment on object() instances raises wrong exception
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 2.7, Python 2.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Jim Fasarakis-Hilliard, brian.curtin, dstanek, ncoghlan, pakal, pitrou, r.david.murray, serhiy.storchaka
Priority: normal Keywords:

Created on 2010-01-08 16:32 by pakal, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (12)
msg97416 - (view) Author: Pascal Chambon (pakal) * Date: 2010-01-08 16:32
It seems we can't assign attributes to "objet" class instances, which don't have a __dict__ :

IDLE 2.6.4      
>>> a = object()
>>> a.abc = 3

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    a.abc = 3
AttributeError: 'object' object has no attribute 'abc'
>>> 

This behaviour seems undocumented, and contradicts the documentation on AttributeError -> normally, a TypeError should be raised instead:

exception AttributeError
    Raised when an attribute reference (see Attribute references) or assignment fails. (When an object does not support attribute references or attribute assignments at all, TypeError is raised.)
msg97420 - (view) Author: Brian Curtin (brian.curtin) * (Python committer) Date: 2010-01-08 17:29
The 3.x documentation[1] has this:
"object does not have a __dict__, so you can’t assign arbitrary attributes to an instance of the object class."
- 2.x doesn't have that same blurb -- it looks like it should.

AttributeError vs. TypeError seems to be the issue here.


FWIW, the way to achieve what you had in your example is something like this:
>>> a = type("my_type", (object,), {})
>>> a.abc = 3


[1] http://docs.python.org/3.1/library/functions.html#object
msg97427 - (view) Author: Pascal Chambon (pakal) * Date: 2010-01-08 18:37
Allright, I suppose it's some kind of optimization ?

We get a proper error when we do :

>>> object.test=1

Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    object.test=1
TypeError: can't set attributes of built-in/extension type 'object'
>>> 

So we'd need a similar error when trying to assign to object instances, too...
I fear I won't be able to help with finding a patch alas, it's a little too deep into the python core for me :p
msg97428 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-01-08 18:38
I don't think it's an optimization. The point is that object is mostly meant to be subclassed, not to be used as a glorified dict.
msg97430 - (view) Author: Pascal Chambon (pakal) * Date: 2010-01-08 18:52
OK, eligible to QOTW    :D
msg97431 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-01-08 18:57
But an object to which you can assign attributes but which has no methods can be useful in a number of contexts.  It's not a glorified dict, because attribute-style access is different from dict-style access.  The main place I have used this (creating my own trivial object subclass) is for passing a duck-typed object in to a function that only needs to access certain attributes to get the correct quack.

Why prevent us from using an object instance for this if there's not a functional reason for it?  Python is supposed to be a Consulting Adults language, after all :).

That said, I suspect that giving object a dict would break various assumptions in the core code, and I have no problem with creating that trivial subclass.  It does have the advantage of providing a more meaningful name in error messages.
msg127173 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2011-01-27 04:05
Object instances don't get a __dict__ by default to save the space the pointer and dict instance would require. Most builtins are missing that pointer. It is also the main memory saving provided by the use of __slots__.

As far as AttributeError vs TypeError goes, the CPython core is actually pretty arbitrary as to which it raises (usually for historical reasons). In this case, AttributeError is technically correct, since object.__setattr__ *does* support writable attributes. It just so happens that a bare object() instance doesn't have any (since __doc__ is read-only).

I asked Guido ages ago about cleaning some of this up (due to some discrepancies between __enter__ and __exit__ and other special methods), but he was of the opinion that the backwards compatibility hassles weren't worth the gain in consistency.
msg127174 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2011-01-27 04:18
Along the lines of RDM's last post, see:
http://code.activestate.com/recipes/52308-the-simple-but-handy-collector-of-a-bunch-of-named/

Or, if you do a few web searches for terms like "bethard coghlan namespaces" or "bethard generic objects" you'll get results along the lines of:
http://mail.python.org/pipermail/python-list/2005-February/305129.html

collections.namedtuple is a simpler alternative to most of these ideas, though.
msg281030 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-11-17 10:13
Could this issue be closed now?
msg281031 - (view) Author: Pascal Chambon (pakal) * Date: 2016-11-17 10:26
I guess it can, since retrocompatibility prevents some normalization here.

Or is it worth updating the doc about "AttributeError", misleading regarding the type of exception expected in this case ?
msg291138 - (view) Author: Jim Fasarakis-Hilliard (Jim Fasarakis-Hilliard) * Date: 2017-04-04 20:22
I don't think a change is actually needed here (bumping to decide the fate of this issue)
msg291141 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-04-04 21:18
Agreed.  Time to close this.
History
Date User Action Args
2022-04-11 14:56:56adminsetgithub: 51908
2017-04-04 21:18:07r.david.murraysetstatus: open -> closed
resolution: not a bug
messages: + msg291141

stage: needs patch -> resolved
2017-04-04 20:22:08Jim Fasarakis-Hilliardsetnosy: + Jim Fasarakis-Hilliard
messages: + msg291138
2016-11-17 10:26:43pakalsetstatus: pending -> open

messages: + msg281031
2016-11-17 10:13:06serhiy.storchakasetstatus: open -> pending
nosy: + serhiy.storchaka
messages: + msg281030

2011-01-27 04:18:14ncoghlansetnosy: ncoghlan, pitrou, dstanek, pakal, r.david.murray, brian.curtin
messages: + msg127174
2011-01-27 04:05:49ncoghlansetnosy: + ncoghlan
messages: + msg127173
2011-01-26 17:53:06dstaneksetnosy: + dstanek
2010-01-08 18:57:51r.david.murraysetnosy: + r.david.murray
messages: + msg97431
2010-01-08 18:52:21pakalsetmessages: + msg97430
2010-01-08 18:38:49pitrousetnosy: + pitrou
messages: + msg97428
2010-01-08 18:37:43pakalsetmessages: + msg97427
2010-01-08 17:29:53brian.curtinsetversions: + Python 2.7
nosy: + brian.curtin
title: Problems with attribute assignment on object instances -> Attribute assignment on object() instances raises wrong exception
messages: + msg97420


stage: needs patch
2010-01-08 16:32:11pakalcreate