[Python-Dev] Allow tuple unpacking in return and yield statements

Terry Reedy tjreedy at udel.edu
Sat Nov 25 18:22:41 EST 2017


On 11/25/2017 1:55 AM, David Cuthbert wrote:
> First time contributing back -- if I should be filing a PEP or something like that for this, please let me know.

I don't think a PEP is needed.

> Coming from https://bugs.python.org/issue32117, unparenthesized tuple unpacking is allowed in assignments:
> 
>      rest = (4, 5, 6)
>      a = 1, 2, 3, *rest

Because except for (), it is ',', not '()' that makes a tuple a tuple.

> but not in yield or return statements (these result in SyntaxErrors):
> 
>      return 1, 2, 3, *rest
>      yield 1, 2, 3, *rest

To be crystal clear, a parenthesized tuple with unpacking *is* valid.

       return (1, 2, 3, *rest)
       yield (1, 2, 3, *rest)

So is an un-parenthesized tuple without unpacking.

Since return and yield are often the first half of a cross-namespace 
assignment, requiring the () is a bit surprising.  Perhaps someone else 
has a good reason for the difference.  Otherwise, +1 on the change.

> The unpacking in assignments was enabled by a pre-3.2 commit that I haven't yet been able to track back to a discussion, but I suspect this asymmetry is unintentional. Here's the original commit:
> https://github.com/python/cpython/commit/4905e80c3d2f6abb613d212f0313d1dfe09475dc
> 
> I've submitted a patch (CLA is signed and submitted, not yet processed), and Serihy said that since it changes the grammar I should have it reviewed here and have signoff by the BDFL.




> While I haven't had a need for this myself, it was brought up by a user on StackOverflow (https://stackoverflow.com/questions/47272460/python-tuple-unpacking-in-return-statement/47326859).
> 
> Thanks!
> Dave
> 
> 


-- 
Terry Jan Reedy



More information about the Python-Dev mailing list