On Tue, Sep 1, 2020 at 8:20 AM Christopher Barker <python...@gmail.com>
wrote:

> Thanks -- good to get this written down!
>
> Question: it strikes me that the use case of [] for type hints is
> logically quite different than for indexing. So do they need to use the
> same syntax / dunder?
>
> I think that type hints are used in specific places, and thus the
> interpreter could know if a given [] was a type hint or an indexing
> operation, and thus could dispatch it differently.
>
> Would that be desirable? I'm not sure -- but as type hints' use of [] is
> logically quite different, it might make sense to have it use different
> rules.
>
> There is precedent -- after all, () used to create a tuple follows
> different rules than () used to call a function.
>
> If it's not technically possible to make the distinction, then it's a
> non-issue, but if it is, it may be worth considering.
>

Type hints are indeed dispatched differently, but this is done based on
information that is only available at runtime. Since PEP 560, for `x[y]`,
if no `__getitem__` method is found, and `x` is a type (class) object, and
`x` has a class method `__class_getitem__`, that method is called.
Extending this with keyword args is straightforward. Modifying the compiler
to generate different bytecode for this case is essentially impossible.

See
https://github.com/python/cpython/blob/6844b56176c41f0a0e25fcd4fef5463bcdbc7d7c/Objects/abstract.c#L181-L198
for the code (it's part of PyObject_GetItem).

-- 
--Guido van Rossum (python.org/~guido)
*Pronouns: he/him **(why is my pronoun here?)*
<http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/>
_______________________________________________
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/76QVVMKDCLEO5OUXKCHZV34YI4HZ7BKG/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to