Ivan Semochkin
Ivan Semochkin

Reputation: 8897

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?

Upvotes: 3

Views: 500

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122222

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.

Upvotes: 5

Related Questions