>> On Apr 15, 2020, at 14:08, Caleb Donovick <donov...@cs.stanford.edu> wrote:
>> 
>> Besides performance, I don’t think it fits with Guido’s conception of the 
>> protocols as being more minimal than the builtin types—e.g., set has not 
>> just a & operator, but also an intersection method that takes 0 or more 
>> arbitrary iterables; the set protocol has no such method, so 
>> collections.abc.Set neither specifies nor provides an intersection method). 
>> It’s a bit muddy of a conception at the edges, but I think this goes over 
>> the line, and maybe have been explicitly thought about and rejected for the 
>> same reason as Set.intersection.
> 
> Making __missing__ a first class part of how __getitem__ seems more analogous 
> to __getattr__ and __getattribute__ than the intersection method.

That’s a good point, but…

>  Is there any other dunder that is only implicitly called on builtin types?

Actually, IIRC, __getattr__ is itself another example. Python never calls 
__getattr__, only __getattribute__. However, object.__getattribute__ (and 
type.__getattribute__) calls __getattr__ on failure, the exact same way 
dict.__getitem__ calls __missing__ on failure. (Obviously there are differences 
in practice—e.g., every object is-a object but not every mapping is-a dict, and 
it’s pretty rare to implement __getattribute__ without super… but if you do 
implement __getattribute__ without super, or do the equivalent in a C extension 
class, __getattr__ doesn’t get called, just like __missing__; I’m pretty sure 
the docs explain this somewhere.)

But there are fallbacks that really are a “first class part of the protocol” as 
you intended: iteration falls back to __getitem__, construction calls __init__ 
if __new__ returns an instance, addition tries __radd__ before and/or after 
__add__, etc., and that’s all definitely part of the protocol, baked into the 
interpreter itself, not just part of how some class implements the protocol. So 
your proposal is perfectly coherent and reasonable (at least if you change it 
from “like __getattr__” to “like __radd__” or something), and this is really 
just a footnote.

But I think I’m still against it, both for the “protocols as minimal as 
possible” reason, and because there’s no obvious way to make it a first class 
part of the protocol for mappings without making it a first class part of the 
protocol for sequences (because at the level of the interpreter, and the 
syntax, they’re both the same subscription protocol).

As a side note to the footnote, check out the pickle protocol. IIRC, the 
fallback from __reduce_ex__ to __reduce__ is a first-class part of the protocol 
(like iteration, construction, and arithmetic), but the fallback from 
__getnewargs__ or __getstate__ is just part of some builtin types’ 
implementations (like attribution and subscription).
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/AOJA2LRC4Z3S3CTZGHXXKAVKPMJUXSLM/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to