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

MRAB python at mrabarnett.plus.com
Sat Jun 22 20:26:54 CEST 2013


On 22/06/2013 11:27, Anders Hovmöller wrote:
> 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}

Shouldn't that mean:

     d = {a:a, b:b, c: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?
>
I'm not convinced.



More information about the Python-ideas mailing list