[Python-Dev] q about __dict__
Stepan Koltsov
yozh@mx1.ru
Tue, 13 Aug 2002 17:03:21 +0400
Hi, Guido, other python developers and other subscribers :-)
Anybody, please explain me, is this a bug? :
Code
=== begin ===
class Dict(dict):
def __setitem__(*x):
raise Exception, "Please, do not touch me!"
class A:
def __init__(*x):
self.__dict__ = Dict()
A().x = 12
=== end ===
doesn't raise Exception, i. e. my __setitem__ function is not called.
Patching that is simple: in Objects/classobject.c need to replace
PyDict_SetItem -> PyObject_SetItem
PyDict_GetItem -> PyObject_GetItem
PyDict_DelItem -> PyObject_DelItem
and maybe something else (not much).
Overhead is minimal, and as bonus python gets ability of assigning
object of any type (not inherited from dict) to __dict__.
Motivation:
Somethimes I want to write strange classes, for example, class with
ordered attributes. I know, that it is possibe to implement this
redefining class attributes __setattr__, etc., but setting __dict__
looks clearer.
Thanks for reading this letter till the end ;-)
--
mailto: Stepan Koltsov <yozh@mx1.ru>