assigning a custom mapping type to __dict__
Duncan Booth
duncan.booth at invalid.invalid
Tue Mar 1 07:25:39 EST 2005
Daniel Cer wrote:
> Why not just inherit from dict? That seems to work.
>
> >>> class M(dict):
> ... def __getitem__(self,key):
> ... return 42
> ... def __setitem__(self,key,value):
> ... pass
> ...
> >>> class C(object):
> ... pass
> ...
> >>> c = C()
> >>> c.__dict__ = M()
> >>> c.__dict__['x']
> 42
>
Didn't test this very much, did you?
>>> c.x
Traceback (most recent call last):
File "<pyshell#23>", line 1, in -toplevel-
c.x
AttributeError: 'C' object has no attribute 'x'
Or even:
>>> c = C()
>>> c.__dict__ = M({'x': 1})
>>> c.x
1
>>> c.__dict__['x']
42
>>>
More information about the Python-list
mailing list