[Python-ideas] Short form for keyword arguments and dicts
Anders Hovmöller
boxed at killingar.net
Sat Jun 22 21:23:03 CEST 2013
Well no, because:
foo(**dict(a=a, b=b, c=c))
foo(**{'a':a, 'b':b, 'c':c})
foo(a=a, b=b, c=c)
are all the same thing. So the short forms should match in the same way:
foo(**dict(=a, =b, =c))
foo(**{:a, :b, :c})
foo(=a, =b, =c)
On Sat, Jun 22, 2013 at 8:26 PM, MRAB <python at mrabarnett.plus.com> wrote:
> 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<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.
>
> ______________________________**_________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/**mailman/listinfo/python-ideas<http://mail.python.org/mailman/listinfo/python-ideas>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130622/5663a40a/attachment.html>
More information about the Python-ideas
mailing list