Python Glossary Русский

Python 3.3+typesterm

types.MappingProxyType

namespaces

A read-only view of a dict: it cannot be changed through the proxy, but edits to the underlying dict show up at once. A class __dict__ is one of these.

Syntax

types.MappingProxyType(mapping)

A proxy is neither a copy nor a freeze. Whoever holds the original dict can still change it and the proxy will show it; mutable values inside (lists) can be edited freely too. It suits exposing a module's settings without write access. Need an independent copy — dict(proxy); need an immutable dict — frozendict in 3.15 or a tuple of pairs.

Examples

from types import MappingProxyType
settings = {'debug': False}
view = MappingProxyType(settings)
try:
    view['debug'] = True
except TypeError:
    print('TypeError')  # → TypeError
settings['debug'] = True
print(view['debug'])  # → True

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy