Issue 1015792: setitem for dict ignored (original) (raw)

Issue1015792

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:06 admin set github: 40821
2004-08-25 05:50:00 vdanilov create