What is a function parameter =[] for?

Ian Kelly ian.g.kelly at gmail.com
Fri Nov 20 11:16:45 EST 2015


On Fri, Nov 20, 2015 at 5:28 AM, Marko Rauhamaa <marko at pacujo.net> wrote:
> The Ackermann function really is an esoteric example, but the other
> example that has been discussed here can make practical use of the
> default-value semantics:
>
>    [ lambda x: i * x for i in range(4) ]
>
> which is salvaged with a default value:
>
>    [ lambda x, i=i: i * x for i in range(4) ]
>
> or, more hygienically:
>
>    [ (lambda i=i: lambda x: i * x)() for i in range(4) ]

Note that this last version could be rewritten as:

    [ (lambda i: lambda x: i * x)(i) for i in range(4) ]

At which point the default value semantics are no longer needed.

> One could argue that you should always use a sentinel object for default
> values. That also allows you to distinguish between omitted values and
> default values:
>
>    def asklist(caption, data, n=omitted, rows=omitted, width=omitted,
>                flags=omitted, buttons=omitted, tablist=omitted,
>                heading=omitted):
>
> but that would be rather pedantic in most circumstances.

I think that would be bad design; in general, you shouldn't *need* to
distinguish whether the value was omitted, because it should always be
possible to explicitly pass the default value.



More information about the Python-list mailing list