[Python-ideas] Short form for keyword arguments and dicts

Nick Coghlan ncoghlan at gmail.com
Sun Jun 23 15:05:49 CEST 2013


On 23 Jun 2013 18:23, "Anders Hovmöller" <boxed at killingar.net> wrote:
>
>
>> You initially said that ObjC deals with this problem better than Python,
and now you say that it's better because it forces you to use the keyword
names (actually they're part of the method name, but let's ignore that)
_always_, which Python only forces you to do it when not using them
positionally.
>>
>> I don't understand why you're making this argument in support of a
proposal that would make Python even less explicit about keyword names,
less like ObjC, and, by your analysis, harder to maintain and therefore
worse.
>
>
> I think you and I are talking about different things when talking about
"this problem". For me the problem is to avoid stuff like "foo(1, 'foo',
None, 9, 'baz')", not avoid repeating names. I just believe that python has
syntax that promotes positional arguments even when it makes the code
worse. My suggestion might on the surface look like just a way to type
less, but that misses the point. It's about shifting the balance towards
keyword arguments.

Then use Python 3 and declare your functions with keyword-only arguments.
That means your APIs can no longer be invoked with positional arguments.
You can do the same in Python 2 by accepting arbitrary kwargs and unpacking
them with an inner function or retrieving them directly from the dictionary.

We do this ourselves in the standard library for APIs where we expect it to
significantly improve clarity at call sites (consider the "key" and
"reverse" arguments to sorted and list.sort).

Cheers,
Nick.

>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130623/617bb4c3/attachment.html>


More information about the Python-ideas mailing list