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: __setitem__ for __dict__ ignored
Type: Stage:
Components: Interpreter Core Versions: Python 2.3
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: nnorwitz, rhettinger, vdanilov
Priority: normal Keywords:

Created on 2004-08-25 05:50 by vdanilov, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
test.py vdanilov, 2004-08-25 05:50 Example
Messages (3)
msg22207 - (view) Author: Viktor A Danilov (vdanilov) Date: 2004-08-25 05:50
In python 2.3.3 this code cause assertion:

class d(dict):
    _is_setted = None
    def __setitem__(self, k, v):
        self._is_setted = 1
 
class test:
    def __init__(self):
        self.__dict__ = d()
         
t = test()
t.x = 2
assert t.__dict__._is_setted == 1, 'no `__setitem__` calls'

msg22208 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2004-08-25 06:30
Logged In: YES 
user_id=80475

Underneath the hood, test's setattr is accessing the
dictionary directly instead of going through its __setattr__
slot.

While this one is fixable, I'm not sure it's really a bug. 
There are no documentation promises that things like setattr
won't make direct accesses to the underlying dictionary. 
Python's implementation has tons of direct access code --
the reasons include clarity, speed, avoidance of hard to
debug code paths, or just not having been looked at in this way.

In any case, I would not have expected old style classes
like 'test' to know about new style subclasses.

For the use cases modeled after the OP's code, overiding
setattr in 'test' is likely the way to go.
msg22209 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2004-08-26 04:43
Logged In: YES 
user_id=33168

I agree with Raymond.  I think this pretty much the design
and should not be fixed.  Therefore, I'm closing this report.

Viktor, if you disagree, you might try discussing this on
comp.lang.python or brining up this issue on python-dev. 
Feel free to comment here if you would like clarifications.  
History
Date User Action Args
2022-04-11 14:56:06adminsetgithub: 40821
2004-08-25 05:50:00vdanilovcreate