[Python-Dev] Allow tuple unpacking in return and yield statements
Guido van Rossum
gvanrossum at gmail.com
Sat Nov 25 18:26:45 EST 2017
I think the proposal is reasonable and won't require a PEP.
On Nov 25, 2017 3:25 PM, "Terry Reedy" <tjreedy at udel.edu> wrote:
> 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/4905e80c3d2f6abb613
>> d212f0313d1dfe09475dc
>>
>> 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
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: https://mail.python.org/mailman/options/python-dev/guido%
> 40python.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20171125/bc8fab44/attachment.html>
More information about the Python-Dev
mailing list