[Python-ideas] Expose a child factory using MappingProxyType in builtins

Victor Stinner victor.stinner at gmail.com
Tue Feb 28 17:19:39 EST 2017


2017-02-28 13:17 GMT+01:00 Michel Desmoulin <desmoulinmichel at gmail.com>:
> We have the immutable frozenset for sets and and tuples for lists.
>
> But we also have something to manipulate dict as immutable datastructures:
>
>>>> from types import MappingProxyType as idict
>>>> d = idict({'a':1, 'b':2, 'c':3})
>>>> d['a'] = 4
> Traceback (most recent call last):
>   File "<ipython-input-3-c6f93d6278af>", line 1, in <module>
>     d['a'] = 4
> TypeError: 'mappingproxy' object does not support item assignment
>
> We could expose this as a built type to allow the last of the most
> important data structure in Python to be easily immutable.

Hello,

Such builtin type exists! It's called types.MappingProxyType!

... Sorry, I don't understand your proposition. Do you want to not
have to write "from types import MappingProxyType" and put a new type
in __builtins__? If yes, I don't think that it's worth it, it's not
that hard to add an import to each file using such type.

By the way, try to not abuse this type please ;-) (Don't try to put it
everywhere?)

Victor


More information about the Python-ideas mailing list