[Python-ideas] Accepting keyword arguments for __getitem__
Stefano Borini
stefano.borini at ferrara.linux.it
Mon Jun 23 17:59:11 CEST 2014
On Mon, Jun 23, 2014 at 08:01:19AM -0500, Ian Cordasco wrote:
> Chris may have missed that requirement (as I did) when they first read
> your email.
I blame my poor choice of subject on that misunderstanding. Apologies.
> Your desired behaviour matches no other known behaviour in
> Python. The only way to achieve that would be to do something akin to:
>
> foo(dict(z=3), dict(r=4))
>
> And the same would be true of your proposed feature for __getitem__
> because all keyword arguments would be collected into one dictionary.
> It would be unreasonable for just one method to behave totally
> differently from the standard behaviour in Python.
> It would be confusing for only __getitem__ (and ostensibly, __setitem__) to take
> keyword arguments but instead of turning them into a dictionary, turn
> them into individual single-item dictionaries.
I tend to agree, however, the fact is that when you say
a[2,3,4]
__getitem__ is not called with four arguments. It's called with one tuple
argument, which puts it already in a different category than a(2,3,4), where
each entry is bound to individual arguments. It makes sense if you
understand
the comma as a tuple production. With keyword arguments, it would
resemble more
of a namedtuple, at least partially.
The alternative, and accidentally proposed by my subject, would be to have
__getitem__(self, y, **kwargs) and have a[1,2,Z=3,R=4] produce
y=(1,2)
kwargs = {"Z":3, "R": 4}
but that would be equally heterogeneous (no *args), and it would not
preserve ordering.
I am not a big fan either of my own idea. I just threw a bone to see if
it has
already been discussed or if anyone would envision other possible use
cases for
this notation.
More information about the Python-ideas
mailing list