
On 22/06/13 20: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)
"Often"? In my experience, it's more like "very occasionally", and even then usually only one or two arguments: foo(spam, "yummy meatlike substance", foobaz=eggs)
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-keyw... )
All very well and good, but you'll notice that in the entire discussion about keywords, with many, many examples give, there was *one* comment about matching names. In practice I don't believe this happens often enough to be more than an occasional nuisance. Not even a nuisance really. There is a moment of micro-surprise "oh look, a name is repeated" but that's all.
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)
Ewww, that's hideous, and far worse than the (non-)problem you are trying to solve. But even if you disagree about the ugliness of code written in that form, consider what a special case you are looking at: given a function that takes an argument "eggs", there is an infinite number of potential arguments you *could* give. You can give arguments in positional form or keyword form, as literals, expressions, or as a single name. Just the names alone, there is a (near enough to) infinite number of possibilities: function(eggs=a) function(eggs=b) function(eggs=x) function(eggs=counter) function(eggs=flag) function(eggs=sequence) function(eggs=value) function(eggs=spam) function(eggs=ham) function(eggs=foo) function(eggs=bar) Out of this vast number of possible names that might be given, you wish to introduce new syntax to cover just one single special case: spam(eggs=eggs) => spam(=eggs) The Zen of Python has not one but two koans that cover this idea: py> import this [...] Explicit is better than implicit. Special cases aren't special enough to break the rules. You would invent new syntax, which requires more complexity in the parser, more tests, more documentation, yet another thing for people to learn, for such an utterly tiny and marginal decrease in typing that even if we agreed it was worthwhile in that special case, it is (in my opinion) hardly worth the effort. So: - I don't believe this is a problem that needs to be solved; - Even if it is a problem, I don't believe your proposal is a good solution. -- Steven