Why this code works in python3, but not python 2:

alex23 wuwei23 at gmail.com
Thu Jul 4 00:47:03 EDT 2013


On 4/07/2013 2:12 PM, Joshua Landau wrote:
> On 4 July 2013 04:52, Maciej Dziardziel <fiedzia at gmail.com> wrote:
>> def foo(*args, bar=1, **kwargs):
>>      pass

> Try "foo(1)" and it will fail -- "bar" needs to be given as a keyword.

No it won't, because it is supplied with a default. You may be
confusing it with the requirement that `bar` must be given as a keyword
in order for it to override the default, compared to the way this would
need to be written in Py2:

     def foo(bar=1, *args, **kwargs):
         ...

...in which case `bar` could be assigned to by the first positional
argument.



More information about the Python-list mailing list