Re: [Python-Dev] Add a frozendict builtin type

2012年3月01日 06:52:38 -0800

> But I will try to suggest another approach. `frozendict` inherits from
> `dict`, but data is not stored in the parent, but in the internal
> dictionary. And even if dict.__setitem__ is used, it will have no visible
> effect.
>
> class frozendict(dict):
>   def __init__(self, values={}):
>     self._values = dict(values)
>   def __getitem__(self, key):
>     return self._values[key]
>   def __setitem__(self, key, value):
>     raise TypeError ("expect dict, got frozendict")
>   ...
I would like to implement frozendict in C to be able to pass it to
PyDict_GetItem(), PyDict_SetItem() and PyDict_DelItem(). Using such
Python implementation, you would get surprising result:
d = frozendict()
dict.__setitem__(d, 'x', 1) # this is what Python does internally when
it expects a dict (e.g. in ceval.c for __builtins__)
'x' in d => False
(Python is not supposed to use the PyDict API if the object is a dict
subclass, but PyObject_Get/SetItem.)
Victor
_______________________________________________
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to