msg171627 - (view) |
Author: (GlitchMr) |
Date: 2012-09-30 11:07 |
Tuple extraction in lambda isn't supported with more than one argument. |
|
|
msg171634 - (view) |
Author: Ramchandra Apte (Ramchandra Apte) * |
Date: 2012-09-30 13:03 |
Python 2.7 and earlier are in bug-fix mode (means no new features) Tuple unpacking is not there in Python 3. Please close this bug as invalid. |
|
|
msg171635 - (view) |
Author: Mark Dickinson (mark.dickinson) *  |
Date: 2012-09-30 13:08 |
Works for me (Python 2.7.3): >>> x = lambda (a, b), c: a + b + c >>> x((2, 3), 4) 9 What issue are you seeing? |
|
|
msg171636 - (view) |
Author: Serhiy Storchaka (serhiy.storchaka) *  |
Date: 2012-09-30 13:20 |
The issue is in lib2to3. tuple_params does not work with lambdas. I confirm the bug. $ ./python -m lib2to3 -f tuple_params lambda_tuple_params.py RefactoringTool: Refactored lambda_tuple_params.py --- lambda_tuple_params.py (original) +++ lambda_tuple_params.py (refactored) @@ -1,3 +1,3 @@ lambda (a, b), c: a + b + c -def f((a, b), c): return a + b + c +def f(xxx_todo_changeme, c): (a, b) = xxx_todo_changeme; return a + b + c RefactoringTool: Files that need to be modified: RefactoringTool: lambda_tuple_params.py |
|
|
msg171768 - (view) |
Author: Raymond Hettinger (rhettinger) *  |
Date: 2012-10-02 01:38 |
I'm not sure a semantically neutral automatic fix is possible: f = lambda (a, b), c: a + b + c # Py2.x f = lambda t, c: t[0] + t[1] + c # Py3.x The former will unpack any iterable, not just sequences: >>> def g(): yield 'a' yield 'b' >>> f(g(), 'c') 'abc' Also, the former will validate the number of arguments: >>> f((1,2,3), 4) Traceback (most recent call last): File "<pyshell#11>", line 1, in f((1,2,3), 4) File "<pyshell#0>", line 1, in f = lambda (a, b), c: a + b + c ValueError: too many values to unpack I don't see a way to automatically include those capabilities in an automatic 2-to-3 transformation. |
|
|
msg171779 - (view) |
Author: Serhiy Storchaka (serhiy.storchaka) *  |
Date: 2012-10-02 09:06 |
f = lambda t, c, *, _f=(lambda a, b, c: a + b + c): _f(*(unpack_tuple(2, t) + (c,))) def unpack_tuple(n, t): t = tuple(t) if len(t) > n: raise ValueError('too many values to unpack (expected %d)' % (n,)) if len(t) < n: raise ValueError('need more than %d values to unpack' % (len(t),)) return t |
|
|
msg171846 - (view) |
Author: Raymond Hettinger (rhettinger) *  |
Date: 2012-10-03 01:37 |
Not pretty :-) IMO, we're better off leaving 2-to-3 without an automated conversion for this and people will just have to stop using the feature in their 2.7 code. We could add a -3 warning in Py2.7 to flag code that needs to be changed. |
|
|
msg379319 - (view) |
Author: Irit Katriel (iritkatriel) *  |
Date: 2020-10-22 17:58 |
Updating the versions, but from the discussion I think this can probably be closed as "won't fix". |
|
|