[Python-ideas] Override dict.__new__ to raise if cls is not dict; do the same for str, list, etc.
Steven D'Aprano
steve at pearwood.info
Fri Apr 22 07:16:52 EDT 2016
On Fri, Apr 22, 2016 at 12:45:34AM -0400, Random832 wrote:
[...]
> My main concern here is the class dict. So, let's see...
>
> ### Class attribute references are translated to lookups in this
> dictionary, e.g., C.x is translated to C.__dict__["x"]
>
> Now, that is technically true (C.__dict__ is, we've established, not the
> actual dict, but a "mappingproxy" object), but this behavior itself
> contradicts the documentation:
>
> ### [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.
> 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.
--
Steve
More information about the Python-ideas
mailing list