[Python-ideas] Fwd: Fwd: unpacking generalisations for list comprehension
Chris Angelico
rosuav at gmail.com
Sat Oct 15 22:55:52 EDT 2016
On Sun, Oct 16, 2016 at 12:10 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> On Sun, Oct 16, 2016 at 04:36:05AM +1100, Chris Angelico wrote:
>> On Sun, Oct 16, 2016 at 4:33 AM, אלעזר <elazarg at gmail.com> wrote:
>> > You are confusing here two distinct roles of the parenthesis: disambiguation
>> > as in "(1 + 2) * 2", and tuple construction as in (1, 2, 3). This overload
>> > is the reason that (1) is not a 1-tuple and we must write (1,).
>>
>> Parentheses do not a tuple make. Commas do.
>>
>> 1, 2, 3, # three-element tuple
>> 1, 2, # two-element tuple
>> 1, # one-element tuple
>>
>> The only time that a tuple requires parens is when it's the empty tuple, ().
>
> Or to disambiguate a tuple from some other comma-separated syntax. Hence
> why you need the parens here:
>
> [(b, a) for a,b in sequence]
Yes, in the same way that other operators can need to be
disambiguated. You need to say (1).bit_length() because otherwise "1."
will be misparsed. You need parens to say x = (yield 5) + 2, else it'd
yield 7. But that's not because a tuple fundamentally needs
parentheses.
ChrisA
More information about the Python-ideas
mailing list