[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:03:35 EDT 2016

On Fri, Apr 22, 2016 at 03:17:34PM +1000, Ben Finney wrote:
> Random832 <random832 at fastmail.com> writes:
> > On Thu, Apr 21, 2016, at 23:49, Steven D'Aprano wrote:
> > > Where does attribute lookup come into this?
> >
> > Because looking up an attribute implies getting an item from the
> > object or class's __dict__ with a string key of the name of the
> > attribute (see below for the documented basis for this assumption).
> No, that's not what it implies. The ‘__dict__’ of an object is an
> implementation detail, and is not necessarily used for attribute lookup.

I think it's a bit more than an implementation detail. For example, I 
don't think it would be legitimate for (let's say) FooPython to decide 
to use instance.__tree__ (a binary tree) for instance attribute 

But I think that the details are far more complex than "attribute 
lookups are done by key access to a dict __dict__" (for example, if 
attribute lookups are done by inspecting obj.__dict__, how do you look 
up obj.__dict__?). And I don't think that the docs suggest that they are 
replacable by dict subclasses.

> > How are you not following this? I don't believe you're not messing
> > with me.
> Steven follows quite well; he is trying to get you to explain your
> meaning so we can find where the mismatch is.

Not quite: my first email was genuinely confused. When I said I didn't 
understand what Random was trying to say, I meant it. By my second 
email, or at least the end of it, I could guess what he was trying to 
do, which (I think) is to replace __dict__ with a subclass instance in 
order to customise attribute lookup.


More information about the Python-ideas mailing list