New submission from Serhiy Storchaka <storchaka+cpyt...@gmail.com>:

The purpose of MappingProxyType is to provide a read-only proxy for mapping. It 
should not expose the underlying mapping because it would invalidate the 
purpose of read-only proxy. But there is a way to do this using comparison 
operator:

from types import MappingProxyType

orig = {1: 2}
proxy = MappingProxyType(orig)

class X:
    def __eq__(self, other):
        other[1] = 3

assert proxy[1] == 2
proxy == X()
assert proxy[1] == 3
assert orig[1] == 3

In particularly it allows to modify __dict__ of builtin types.

----------
components: Interpreter Core
messages: 391039
nosy: rhettinger, serhiy.storchaka
priority: normal
severity: normal
status: open
title: There is a way to access an underlying mapping in MappingProxyType
type: behavior
versions: Python 3.10, Python 3.8, Python 3.9

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue43838>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to