From: MRAB on
Xavier Ho 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 test(a = 1, b = (2, 3)):
> ... print a, b
> ...
>> >> test()
> 1 (2, 3)
>
>> >> 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?
>
Perhaps it was that no-one ever thought of doing that! :-)
From: Chris Rebert on
On Sun, Apr 18, 2010 at 4:23 PM, Xavier Ho <contact(a)xavierho.com> wrote:
> I ran into a strange problem today: why does Python not allow default
> paranmeters for packed arguments in a function def?
>
>>>> def test(a = 1, b = (2, 3)):
> ...     print a, b
> ...
>>>> test()
> 1 (2, 3)
>
>>>> 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?

It's not specific to default arguments:

*z = (1,2,3) #==>

File "<stdin>", line 1
*z = (1,2,3)
SyntaxError: starred assignment target must be in a list or tuple

It doesn't really make sense to use * in such situations anyway, you
can just do the normal `z = (1,2,3)`.

Cheers,
Chris
--
http://blog.rebertia.com
From: alex23 on
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)
...
From: Gregory Ewing on
MRAB wrote:
> Xavier Ho wrote:
>
>>> >> 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?

The concept of a default value for the * argument doesn't
really apply, because there is always a value for it, even
if it's just an empty tuple.

--
Greg
From: Terry Reedy on
On 4/18/2010 7:23 PM, Xavier Ho 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 test(a = 1, b = (2, 3)):
> ... print a, b
> ...
>> >> test()
> 1 (2, 3)
>
>> >> 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?

To reword what G. Ewing said, because t(3) causes b to be empty. In
actually use cases, I believe that that is usually the appropriate thing
to do.

tjr