[Python-ideas] Override dict.__new__ to raise if cls is not dict; do the same for str, list, etc.

Eric Snow ericsnowcurrently at gmail.com
Fri Apr 22 15:19:59 EDT 2016

On Fri, Apr 22, 2016 at 5:16 AM, Steven D'Aprano <steve at pearwood.info> wrote:
> On Fri, Apr 22, 2016 at 12:45:34AM -0400, Random832 wrote:
>> ### [type] With three arguments, [...] and the dict dictionary is the
>> namespace containing definitions for class body and becomes the __dict__
>> attribute.
>> Except, it *doesn't* become the __dict__ attribute - its contents are
>> *copied* into the __dict__ object, which is a new "mappingproxy" whose
>> contents will not reflect further updates to the dict that was passed
>> in.
> That is a very good point. I think that's a documentation bug.

mappingproxy (a.k.a. types.MappingProxyType) is exactly what it says:
a proxy for a mapping.  Basically, it is a wrapper around a

The namespace (a dict or dict sub-class, e.g. OrderedDict) passed to
type() is copied into a new dict and the new type's __dict__ is set to
a mappingproxy that wraps that copy.

So if there is a documentation bug then it is the ambiguity of the
word "becomes".  Perhaps it would be more correct as "is copied into".

>> And regarding the object __dict__, when such a __dict__ *does* exist
>> (since, unlike class dicts, you actually can set object dicts to be
>> arbitrary dict subclasses)
> True, but the documentation doesn't say that attribute lookup goes
> through the *full* dict key lookup, including support of __missing__ and
> __getitem__. I'll grant you that neither does the documentation say that
> it doesn't, so I'd call this a documentation bug.

What would you say is the specific documentation bug?  That the
default attribute lookup (object.__getattribute()) does not use the
obj.__dict__ attribute but rather the dict it points to (if it knows
about it)?  Or just that anything set to __dict__ is not guaranteed to
be honored by the default __getattribute__()?


More information about the Python-ideas mailing list