On Fri, Jul 10, 2020 at 11:52:19AM +0100, Jonathan Fine wrote:
> FUTURE
> Let's proceed. We continue to use d = Dummy(). Given that
> >>> key = d[1, 2, 3, a=4, b=5]
> is allowed, what should we be able to say about the key. Clearly it should
> be an instance of a class
That's not clear at all.
Subscripting is just syntactic sugar for a method call, `__getitem__`,
and we already know how to associate positional and keyword arguments to
method calls. No special magic class is needed.
Slicing today is somewhat special, using a built-in class to map the
parts of the slice to a single parameter of the `__getitem__` method,
but that's not how it worked originally. In Python 1 and Python 2,
slicing with a single colon calls a dunder method `__getslice__` with
two positional arguments:
seq[1:5] --> seq.__getslice__(1, 5)
not a single slice object argument. Only if `__getslice__` doesn't exist
is a slice object passed to `__getitem__` instead.
So in principle, if we agreed that the syntax was desirable, we could
map keyword arguments in slice syntax to ordinary parameters:
obj[3, spam=4]
# call obj.__getitem__(3, spam=4)
and likewise for setitem and delitem.
There would be difficulty with positional arguments, since they are
already parsed as a tuple:
py> {(1,2): 999}[1,2]
999
so this may rule out adding multiple positional arguments to
subscripting syntax. But I don't think there is any backwards
compatibility issue with adding keyword arguments.
--
Steven
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/D5FWL4MVG4W46BUQMNVFXLJJJMNQ75L6/
Code of Conduct: http://python.org/psf/codeofconduct/