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

Anders Hovmöller boxed at killingar.net
Sat Jun 22 12:27:52 CEST 2013


Keyword arguments are great for increasing readability and making code more
robust but in my opinion they are underused compared to the gains they can
provide. You often end up with code like:

foo(bar=bar, baz=baz, foobaz=foobaz)

which is less readable than the ordered argument version when the names of
the variables and the keywords match. ( Here's another guy pointing out the
same thing:
http://stackoverflow.com/questions/7041752/any-reason-not-to-always-use-keyword-arguments#comment8553765_7041986
)

I have a suggestion that I believe can enable more usage of keyword
arguments while still retaining almost all the brevity of ordered
arguments: if the variable name to the right of the equal sign equals the
keyword argument ("foo=foo") make it optional to just specify the name once
("=foo"). For completeness I suggest also make the same change for
dictionaries: {'foo': foo} -> {:foo}. This change would turn the following
code:

a = 1
b = 2
c = 3
d = {'a':a, 'b':b, 'c':c}
foo(a=a, b=b, c=c)

into:

a = 1
b = 2
c = 3
d = {:a, :b, :c}
foo(=a, =b, =c)


This should be compatible with existing code bases since the new forms are
syntax errors in current python.

What do you think?

/ Anders Hovmöller
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130622/f4273f9e/attachment.html>


More information about the Python-ideas mailing list