Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What the difference between dict_proxy in python2 and mappingproxy in python3?

I notice when I create class in python2 it stores attributes in dict_proxy object:

>>> class A(object):
...     pass
>>> A.__dict__
dict_proxy({....})

But in python3 __dict__ returns mappingproxy:

>>> class A(object):
...     pass
>>> A.__dict__
mappingproxy({....})

Is there any difference between two of them?

like image 231
Ivan Semochkin Avatar asked Jul 14 '26 11:07

Ivan Semochkin


1 Answers

There is no real difference, it just got renamed.

When it was proposed to expose the type in the typing module in issue #14386, the object was renamed too:

I'd like to bikeshed a little on the name. I think it should be MappingProxy. (We don't use "view" much but the place where we do use it, for keys/values/items views, is very different I think. Also collections.abc already defines MappingView as the base class for KeysView and friends.)

and

Anyway, you are not the first one who remarks that we already use "view" to define something else, so I wrote a new patch to use the "mappingproxy" name (exposed as types.MappingProxyType).

The change made it into Python 3.3, so in Python 3.2 you'll still see the old name.

like image 90
Martijn Pieters Avatar answered Jul 17 '26 16:07

Martijn Pieters