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

Ethan Furman ethan at stoneleaf.us
Fri Apr 22 07:58:53 EDT 2016

On 04/22/2016 04:16 AM, Steven D'Aprano wrote:
> On Fri, Apr 22, 2016 at 12:45:34AM -0400, Random832 wrote:

>> ### 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:

No, it's a real dict() -- we just don't get direct access to it anymore.

>> ### [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.

See above about the 'mappingproxy'.  As for updates, IIRC setattr() is 
the way to make changes these days.

> That is a very good point. I think that's a documentation bug.

Patches welcome.  ;)


More information about the Python-ideas mailing list