Chris Angelico writes:

 > I have to defer to someone who knows in more detail here, but the
 > parser will look ahead one "token", whatever a token is. (Technically
 > it doesn't backtrack, it looks ahead - same difference.)

Tokens are basically keywords, identifiers, most single punctuation
characters, and indentation.  Some strings of punctuation are tokens
by special dispensation.

 > In order for the ternary-if to work, the expression before the if
 > has to be of a more restricted type than just "any expression" (for
 > instance, you can't put a lambda function there, unless you
 > parenthesize it).

It's not that it's a restricted expression, it's that a valid lambda
will consist of a syntactically complete expression.  If you then
place "if" after that lambda without parenthesizing, the parser will
think that the lambda contains the whole ternary expression because
pretty much any Python expression has a value that can be implicitly
cast to bool.

Steve

_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to