Default paranmeter for packed values

alex23 wuwei23 at gmail.com
Sun Apr 18 20:45:40 EDT 2010


Xavier Ho wrote:
> 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?

Possibly because what you're calling 'packed arguments' are really
_arbitrary_ arguments, that is, it catches those that aren't defined
in the function signature. If you want default values, you should use
default arguments.

Of course, you can always get around it with this old chestnut:

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



More information about the Python-list mailing list