[Python-ideas] Accepting keyword arguments for __getitem__

Ian Cordasco graffatcolmingov at gmail.com
Mon Jun 23 15:01:19 CEST 2014


On Mon, Jun 23, 2014 at 7:53 AM, Stefano Borini
<stefano.borini at ferrara.linux.it> wrote:
> On Mon, Jun 23, 2014 at 10:24:53PM +1000, Chris Angelico wrote:
>> The obvious way to accept that would be to support keyword arguments,
>> and then it begins looking very much like a call. Can you alter your
>> notation very slightly to become LDA(Z=5) instead?
>
> We certainly can, but I was wondering if such extension would be useful in other contexts.
> Also, with the function solution, you would lose the order of the entries. You can't distinguish
> foo(z=3, r=4) from foo(r=4, z=3)

Chris may have missed that requirement (as I did) when they first read
your email. 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.


More information about the Python-ideas mailing list