[ python-Bugs-1015792 ] __setitem__ for __dict__ ignored

SourceForge.net noreply at sourceforge.net
Thu Aug 26 06:43:28 CEST 2004


Bugs item #1015792, was opened at 2004-08-25 01:50
Message generated for change (Settings changed) made by nnorwitz
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1015792&group_id=5470

Category: Python Interpreter Core
Group: Python 2.3
>Status: Closed
>Resolution: Wont Fix
Priority: 5
Submitted By: Viktor A Danilov (vdanilov)
Assigned to: Nobody/Anonymous (nobody)
Summary: __setitem__ for __dict__ ignored

Initial Comment:
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'



----------------------------------------------------------------------

>Comment By: Neal Norwitz (nnorwitz)
Date: 2004-08-26 00:43

Message:
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.  

----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2004-08-25 02:30

Message:
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.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1015792&group_id=5470


More information about the Python-bugs-list mailing list