Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #49802 > unrolled thread

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

Started byMaciej Dziardziel <fiedzia@gmail.com>
First post2013-07-03 20:52 -0700
Last post2013-07-04 06:10 +0100
Articles 7 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  Why this code works in python3, but not python 2: Maciej Dziardziel <fiedzia@gmail.com> - 2013-07-03 20:52 -0700
    Re: Why this code works in python3, but not python 2: alex23 <wuwei23@gmail.com> - 2013-07-04 14:05 +1000
      Re: Why this code works in python3, but not python 2: Maciej Dziardziel <fiedzia@gmail.com> - 2013-07-03 21:31 -0700
    Re: Why this code works in python3, but not python 2: Chris Angelico <rosuav@gmail.com> - 2013-07-04 14:10 +1000
    Re: Why this code works in python3, but not python 2: Joshua Landau <joshua.landau.ws@gmail.com> - 2013-07-04 05:12 +0100
      Re: Why this code works in python3, but not python 2: alex23 <wuwei23@gmail.com> - 2013-07-04 14:47 +1000
        Re: Why this code works in python3, but not python 2: Joshua Landau <joshua.landau.ws@gmail.com> - 2013-07-04 06:10 +0100

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

FromMaciej Dziardziel <fiedzia@gmail.com>
Date2013-07-03 20:52 -0700
SubjectWhy this code works in python3, but not python 2:
Message-ID<e0ffc562-4016-4b5d-88c3-5a115da16906@googlegroups.com>
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

[toc] | [next] | [standalone]


#49803

Fromalex23 <wuwei23@gmail.com>
Date2013-07-04 14:05 +1000
Message-ID<kr2rvn$lkc$1@dont-email.me>
In reply to#49802
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/

[toc] | [prev] | [next] | [standalone]


#49808

FromMaciej Dziardziel <fiedzia@gmail.com>
Date2013-07-03 21:31 -0700
Message-ID<fa9b5b1f-7998-49b4-8314-2b461204b9bc@googlegroups.com>
In reply to#49803
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

[toc] | [prev] | [next] | [standalone]


#49805

FromChris Angelico <rosuav@gmail.com>
Date2013-07-04 14:10 +1000
Message-ID<mailman.4201.1372911048.3114.python-list@python.org>
In reply to#49802
On Thu, Jul 4, 2013 at 1:52 PM, Maciej Dziardziel <fiedzia@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

[toc] | [prev] | [next] | [standalone]


#49806

FromJoshua Landau <joshua.landau.ws@gmail.com>
Date2013-07-04 05:12 +0100
Message-ID<mailman.4202.1372911189.3114.python-list@python.org>
In reply to#49802
On 4 July 2013 04:52, Maciej Dziardziel <fiedzia@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.

[toc] | [prev] | [next] | [standalone]


#49810

Fromalex23 <wuwei23@gmail.com>
Date2013-07-04 14:47 +1000
Message-ID<kr2udr$tvv$1@dont-email.me>
In reply to#49806
On 4/07/2013 2:12 PM, Joshua Landau wrote:
> On 4 July 2013 04:52, Maciej Dziardziel <fiedzia@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.

[toc] | [prev] | [next] | [standalone]


#49812

FromJoshua Landau <joshua.landau.ws@gmail.com>
Date2013-07-04 06:10 +0100
Message-ID<mailman.4206.1372914669.3114.python-list@python.org>
In reply to#49810
On 4 July 2013 05:47, alex23 <wuwei23@gmail.com> wrote:
> On 4/07/2013 2:12 PM, Joshua Landau wrote:
>>
>> On 4 July 2013 04:52, Maciej Dziardziel <fiedzia@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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web