This post originated from an RSS feed registered with Ruby Buzz
by Daniel Berger.
Original Post: If Python can do it...
Feed Title: Testing 1,2,3...
Feed URL: http://djberg96.livejournal.com/data/rss
Feed Description: A blog on Ruby and other stuff.
I've been trying to find another language that does implicit keyword arguments to help assuage the fears some folks have with regards to "mixing interface and implementation". At first, the only one I could think of was Oracle PL/SQL - not exactly the poster child for languages you want to cite when defending your ideas.
Last night, however, Evan mentioned that Python does implicit keyword arguments. Since 1.3, actually. I never knew! Here's a quick example:
def foo(a, b, c):
...
foo(1, 2, 3) # a = 1, b = 2, c = 3
foo(a = 1, c = 2, b = 3) # a = 1, b = 3, c = 2
foo(1, c = 3, b = 4) # a = 1, b = 4, c = 3
Note that, like our current proposal, you can mix positional and named parameters. In fact, the rules we came up with for Sydney are identical to the current rules for Python as far as I can tell. But what about *args and **keys? The Python implementation is similar to Matz's proposal:
Now that I'ved spent some time looking at this, and having reviewed Matz's proposal again, I think one of the major items I disliked was the fact that *args was the first argument, while **keys was last. It just doesn't look right, and I still like the (recently suggested) idea of an Arguments class that handles **keys implicitly. But, that still doesn't deal with the issue of explicit declarations or delegation.
I've asked around a bit on #python to see if anyone had any complaints with regards to implicit keywords and delegation issues in particular. None had. At least, none that spoke up. But, after literally *years* of supporting implicit keywords, I think it's safe to say that they would have been removed by now if they had caused delegation or design problems. Let's take a cue, shall we?
I suspect that there's an initial resistance to this idea that I think is analogous to a static language programmer coming to a dynamic language for the first time, and freaking out a bit when the type declaration training wheels are removed. In theory, it could lead to all sorts of bugs/errors/etc. In reality, not only is it not an issue, it makes other things possible you may have never considered before.
Oh, and while I like the idea of using '=' over ':' for keyword arguments, I don't know that it will be possible because, unlike Python, '=' is an expression. But who knows? Evan is a mad genius - maybe he'll find a way. :)
You can read more about how keyword arguments work in Python here