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

Joshua Landau joshua.landau.ws at gmail.com
Thu Jul 4 00:12:26 EDT 2013


On 4 July 2013 04:52, Maciej Dziardziel <fiedzia at gmail.com> wrote:
> Out of curiosity: Does anyone know why the code below is valid in python3, but not python2:
>
> def foo(*args, bar=1, **kwargs):
>     pass

Python 3 gained syntax for keyword-only arguments.

Try "foo(1)" and it will fail -- "bar" needs to be given as a keyword.
This is because it comes after a *-expression. You can also do "def
foo(*, bar=1)" if you want bar to be keyword-only without accepting
any number of positional arguments. Python 2 does not have these, and
doesn't understand the syntax for them.



More information about the Python-list mailing list