Tuple parameter unpacking in 3.x

Nick Craig-Wood nick at craig-wood.com
Sat Oct 4 12:30:04 CEST 2008


Martin Geisler <mg at daimi.au.dk> wrote:

>  I just tried running my code using "python2.6 -3" and got a bunch of
> 
>    SyntaxWarning: tuple parameter unpacking has been removed in 3.x
> 
>  warnings. I've read PEP-3113:
> 
>    http://www.python.org/dev/peps/pep-3113/
> 
>  but I'm still baffled as to why you guys could remove such a wonderful
>  feature?!

I don't think many people will miss tuple unpacking in def statements.

I think the warning is probably wrong anyway - you just need to remove
a few parens...

>    ci.addCallback(lambda (ai, bi): ai * bi)
>    map(lambda (i, s): (field(i + 1), s), enumerate(si))

On
Python 3.0rc1 (r30rc1:66499, Oct  4 2008, 11:04:33)

>>> f = lambda (ai, bi): ai * bi
  File "<stdin>", line 1
    f = lambda (ai, bi): ai * bi
               ^
SyntaxError: invalid syntax

But

>>> f = lambda ai, bi: ai * bi
>>> f(2,3)
6

Likewise

>>> lambda (i, s): (field(i + 1), s)
  File "<stdin>", line 1
    lambda (i, s): (field(i + 1), s)
           ^
SyntaxError: invalid syntax
>>> lambda i, s: (field(i + 1), s)
<function <lambda> at 0xb7bf75ec>
>>>

So just remove the parentheses and you'll be fine.

I have to say I prefer named functions, but I haven't done much
functional programming

   def f(ai, bi):
       return ai * bi
   ci.addCallback(f)

   def f(i, s):
       return field(i + 1), s
   map(f, enumerate(si))

PEP-3113 needs updating as it is certainly confusing here!  2to3 is
doing the wrong thing also by the look of it.

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list