Re: Default value of keyword arguments Lassi Kortela 04 Mar 2020 17:20 UTC

> There's a classic bug in Python programming where a function takes a
> list argument and mutates the argument in some way.  The programmer
> observes that [] is the most common value (empty lists are mutable in
> Python and [] is a constructor), so they set [] as the default.
>
> Unfortunately, when the default is taken and the function mutates the
> list, that mutated list becomes the default for the next call!  The safe
> strategy is to set the default to None (which is a unique object) and
> then include "if arg == None: arg = []" at the top of the function.  At
> the moment, #f is Scheme's None.

Thanks for explaining the problem. This is a textbook example of what
happens when lambda lists are allowed to be fancy :)

In the case of Lisp, and CL in particular, it's not clear whether:

- an implicit closure is created and the expressions generating default
argument values are evaluated in that closure, remembering the old
values (as in Python above).

- whether the arguments are bound in parallel as with `let`, or serially
as with `let*`, and if serially, is the evaluation order indeterminate?

It's an obscure duplication of Lisp's simple and clear `let` syntax.
`let` and #f seem too simple to be the best solution to all our default
argument woes, but I still can't think of a case where they are not.