r/Python • • 24d ago

Meta Consistency of new unpacking syntax in 3.15?

[removed]

31 Upvotes

28 comments sorted by

View all comments

Show parent comment

13

u/latkde Tuple unpacking gone wrong 24d ago

The unpacking is part of the generator syntax. Unpacking does not create a new value category, so it cannot be used within conditionals.

Formally, we are in an “flexible expression” context. Unpacking (a starred expression) can only occur in a flexible expression. The elements of an expression list (such as the contents of a […] literal can be expressions, as can be the value expression in a … for … in … generator expression. So you can say *some_expression for … in …, but not *some_expression if … else *other_expression – the conditional expression syntax doesn't allow flexible expressions there.

That means we can say [*(… if … else …) for … in …] but not [*… if … else *… for … in …].