Default paranmeter for packed values

Stephen Hansen apt.shansen at gmail.com
Sun Apr 18 19:54:30 EDT 2010


On Sun, Apr 18, 2010 at 4:23 PM, Xavier Ho <contact at xavierho.com> wrote:

> G'day Pythoneers,
>
> I ran into a strange problem today: why does Python not allow default
> paranmeters for packed arguments in a function def?
> >>> def t(a, *b = (3, 4)):
>   File "<input>", line 1
>     def t(a, *b = (3, 4)):
>                 ^
> SyntaxError: invalid syntax
>
> What was the rationale behind this design?
>

Why doesn't Python allow this? Because it just doesn't. I'm not sure there's
so much as a rationale why its not that way, but there's no rationale for it
to BE that way.

A default argument is an argument which is optional, but has a default
value. *args and **kwargs are unknown optional additional arguments. I'm not
sure how you can logically combine the two premises. They are sort of
mutually exclusive, the only similarity is both are 'optional'. They're just
entirely different ways to achieve Optionality.

And since no one's ever wanted it that I can ever remember, it seems to be a
rare sort of use-case, which you could probably easily satisfy by:

def t(a, *b):
    if not b:
        b = (2,3)
    ...

Its a similar pattern to using None as the value of an optional argument as
a sentinel, to then assign the real 'default' value to it once in the
function (this pattern is used when said real default value is mutable, of
course).


--S
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100418/a732144f/attachment-0001.html>


More information about the Python-list mailing list