April 21, 2008

Approaching Python 3000: no more automatic tuple parameter unpacking

Just keep in mind that
lambda (x, y): x + y
is no longer possible in Python 3000. You are supposed to write
lambda x_y: x_y[0] + x_y[1]
instead. This also applies to functions, not just to lambdas and the idea belongs to PEP 3113 which forbids automatic tuple unpacking in function parameters. Ugly and inconvenient if you ask me, but there apparently was somebody who kept shooting himself in the leg.

I think that automatic unpacking was rather useful (if used sparingly), especially when you had to do something like
map(lambda (i, (a, b)): i * (a + b),
enumerate(zip(aa, bb))
which is now what ?
map(lambda i_a_b: i_a_b[0] * (i_a_b[1][0] + i_a_b[1][1]),
enumerate(zip(aa, bb))
Eeew...

2 comments:

Paddy3118 said...

I made the same point before and was told that they could gain a lot in the implementation by dropping it.

- Paddy.

Dmitry Dvoinikov said...

I guess we'll have to resort to list comprehension/generator syntax where unpacking is still allowed:

i * (a + b) \
for (i, (a, b)) in enumerate(zip(aa, bb))

Still not as functional and clean as the original example.