[issue15360] Behavior of assigning to __dict__ is not documented
New submission from Davide Rizzo <sorcio@gmail.com>: The documentation (at least the obvious places, see Doc/reference/datamodel.rst) says classes and class instances have the '__dict__' attribute, but nothing is said about what happens when assigning to it (like obj.__dict__ = something). As far as I understand that's undefined behavior and other implementations may not work the same as CPython (and CPython itself behaves differently between versions). I'd submit a documentation patch if I knew how to specify this matter. Maybe just say the behavior is not defined? ---------- assignee: docs@python components: Documentation, Interpreter Core messages: 165538 nosy: davide.rizzo, docs@python priority: normal severity: normal status: open title: Behavior of assigning to __dict__ is not documented versions: Python 2.7, Python 3.3, Python 3.4 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue15360> _______________________________________
Amaury Forgeot d'Arc <amauryfa@gmail.com> added the comment: What kind of behavior are you referring to? Are there really differences between versions? ---------- nosy: +amaury.forgeotdarc _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue15360> _______________________________________
Changes by Arfrever Frehtes Taifersar Arahesis <Arfrever.FTA@GMail.Com>: ---------- nosy: +Arfrever _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue15360> _______________________________________
Davide Rizzo <sorcio@gmail.com> added the comment: Amaury, I don't honestly know, I would have proposed something otherwise. I have been advised on #python (Freenode) not to assign to obj.__dict__ because its behaviour changes between versions and implementations, but I wouldn't know what has changed between CPython version. One obscure thing is what can be assigned to __dict__. For class dicts no assignment is allowed. Object dicts can be assigned dict objects. If you try to assign a non-dict mapping it will complain. However you can assign a dict-derived object, something like: class SillyDict(dict): def __getitem__(self, key): return "hello" obj.__dict__ = SillyDict() Unfortunately accessing the attributes of obj will still use PyDict_* functions, thus ignoring any magic methods. PyPy's behavior is slightly different. A class __dict__ attribute still is read only, but you can assign any mapping object to an object __dict__. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue15360> _______________________________________
Changes by Ezio Melotti <ezio.melotti@gmail.com>: ---------- nosy: +ezio.melotti stage: -> needs patch type: -> enhancement _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue15360> _______________________________________
Changes by Éric Araujo <merwok@netwok.org>: ---------- nosy: +eric.araujo _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue15360> _______________________________________
Changes by Chris Rebert <pybugs@rebertia.com>: ---------- nosy: +cvrebert _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue15360> _______________________________________
Antoine Pitrou <pitrou@free.fr> added the comment: I think assigning to __dict__ is an implementation detail; we shouldn't document it before it is clearly agreed what it should do (which probably deserves asking on python-dev). ---------- nosy: +pitrou _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue15360> _______________________________________
Changes by Malcolm Smith <malcolm.smith@gmail.com>: ---------- nosy: +Malcolm Smith _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue15360> _______________________________________
participants (8)
-
Amaury Forgeot d'Arc
-
Antoine Pitrou
-
Arfrever Frehtes Taifersar Arahesis
-
Chris Rebert
-
Davide Rizzo
-
Ezio Melotti
-
Malcolm Smith
-
Éric Araujo