Hey, I almost forgot one of my favorite shared features between Common Lisp and Python: keyword arguments! A lot of my research code tends to have lots of parameters, especially in object initialization, but often elsewhere too, like in functions for displaying internal state or writing out data, where I’ll have parameters that usually take default values, but occasionally need to be changed. Languages like C/C++/Java allow optional arguments, but you still have to remember the order, and if you want a non-default the 8th optional argument, you have to give values to the 7 args before it in the argument list. This makes for crazy unreadable code, like this:
x = Foo(0,NULL,NULL,NULL,5,0,0,0,NULL,NULL,32);
I don’t know how I’d come back to this code and 6 months and understand what’s going on.
Lisp and Python’s keyword arguments are a great solution for this problem. They allow you to use the parameter name in the function call. In lisp you might write the function definition like this (assuming short variable names for brevity here):
(defun foo (&key (a 0) b c d (e 0) (f 0) (g 0) h i (j 0)) ;; keyword args default to nil, unless another default is ;; specified, as with a, e, f, g, and j. ...)
Then call it like this:
(setq x (foo :e 5 :j 32))
Here all the unspecified parameters get their defaults. Also the parameters can be specified in any order.
Python’s keyword args work basically the same way, but they’re arguably even more powerful. In Python we’d do this:
def foo(a=0, b=None, c=None, d=None, e=0, f=0, g=0, h=None, i=None, j=0): ...
and call it like this:
x = foo(e=5, j=32)
Again, the keyword arguments can be given in any order. You gotta admit this is much more readable than the C++/Java way. The interesting thing is that Python implements keyword args with dictionaries, a primitive data type. They expose that to the programmer, so you can pass a dictionary where you would put keyword arguments:
dict = {'e':5, 'j':32}
x = foo(**dict)
You can even mix-n-match:
dict = { 'j':32 }
x = foo(e=5,**dict)
You can also use a dictionary to hold a variable number of keyword arguments in the function definition, but I won’t go into that here. Anyway, this was a feature of Common Lisp I didn’t think I could give up. Scheme has them as an added syntax in a library, but they don’t seem to be a generally accepted part of the scheme programming paradigm like they are in Python