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

2013-07-03 Thread Maciej Dziardziel
Out of curiosity: Does anyone know why the code below is valid in python3, but 
not python2:

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


-- 
Maciej Dziardziel
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-07-03 Thread alex23

On 4/07/2013 1:52 PM, Maciej Dziardziel 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


It was an explicit syntax change for Python3. You can read about the
reasoning behind it here:

http://www.python.org/dev/peps/pep-3102/

--
http://mail.python.org/mailman/listinfo/python-list


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

2013-07-03 Thread Chris Angelico
On Thu, Jul 4, 2013 at 1:52 PM, Maciej Dziardziel fied...@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

Keyword-only arguments are (IIRC) a Py3-only feature. There are lots
of features that don't work in Python 2 - that's simply the way things
happen with old versions of software.

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-07-03 Thread Joshua Landau
On 4 July 2013 04:52, Maciej Dziardziel fied...@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.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-07-03 Thread Maciej Dziardziel
On Thursday, July 4, 2013 5:05:23 AM UTC+1, alex23 wrote:
 It was an explicit syntax change for Python3. You can read about the
 reasoning behind it here:
 
 http://www.python.org/dev/peps/pep-3102/

Thanks, that was helpful.

Maciej Dziardziel
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-07-03 Thread alex23

On 4/07/2013 2:12 PM, Joshua Landau wrote:

On 4 July 2013 04:52, Maciej Dziardziel fied...@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.
--
http://mail.python.org/mailman/listinfo/python-list


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

2013-07-03 Thread Joshua Landau
On 4 July 2013 05:47, alex23 wuwe...@gmail.com wrote:
 On 4/07/2013 2:12 PM, Joshua Landau wrote:

 On 4 July 2013 04:52, Maciej Dziardziel fied...@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.

Pah! I'm not even thinking.
-- 
http://mail.python.org/mailman/listinfo/python-list