Inconsistency in dictionary behaviour: dict(dict) not calling __setitem__
Almad
bugs at almad.net
Tue Dec 12 13:31:01 EST 2006
Hello,
I discovered this behaviour in dictionary which I find confusing. In
SneakyLang, I've tried to extend dictionary so it visits another class
after something is added:
class RegisterMap(dict):
def __setitem__(self, k, v):
dict.__setitem__(self, k,v)
self[k].visit_register_map(self)
However, when constructing dictionary with dictionary in constructor
like d = RegisterMap({'k':'v'}), __setitem__ is not called, so
workaround is needed:
class RegisterMap(dict):
def __init__(self, *args, **kwargs):
dict.__init__(self, *args, **kwargs)
for k in self:
self.__after_add(k)
def __after_add(self, k):
self[k].visit_register_map(self)
def __setitem__(self, k, v):
dict.__setitem__(self, k,v)
self.__after_add(k)
What is the reason for this behavior? Am I doing something wrong and
better approach is needed? Or should this be considered as minor bug in
Python? (tried this only in 2.4 so far)
Thank You,
Almad
More information about the Python-list
mailing list