djberg96 (djberg96) wrote,

If Python can do it...

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:
def foo(a, *rest, **keys):

foo(1, foo = "hello", bar = "world")

# rest = [1, "hello", "world"]
# keys = {"foo" : "hello", "bar" : "world"}

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

  • Running a remote rails console

    Another tip for myself, courtesy of a co-worker. If you want to run a rails console on a remote app you can use the `ssh -t` option. The -t option…

  • (no subject)

    Mostly a note to self, this is how you can show your current git branch in Windows Powershell. Just add it to your profile.ps1. function prompt{…

  • Building libxml2 and libxslt on Windows

    I had a little trouble building these from source with MSVC so I thought I'd record the issues I faced both so that I can refer back to this post…

  • Post a new comment


    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded