Why isn't this a syntax error in python?
Asked Answered
E

3

54

Noticed a line in our codebase today which I thought surely would have failed the build with syntax error, but tests were passing so apparently it was actually valid python (in both 2.x and 3).

Whitespace is sometimes not required in the conditional expression:

>>> 1if True else 0
1

It doesn't work if the LHS is a variable:

>>> x = 1
>>> xif True else 0
  File "<stdin>", line 1
    xif True else 0
           ^
SyntaxError: invalid syntax

But it does seem to still work with other types of literals:

>>> {'hello'}if False else 'potato'
'potato'

What's going on here, is it intentionally part of the grammar for some reason? Is this odd quirk a known/documented behaviour?

Earley answered 2/6, 2014 at 15:22 Comment(4)
Python names cannot start with digits, so that's one reason. The parser knows that if is a new token.Doubleacting
@MartijnPieters That's true, but that doesn't really explain what exactly is happening.Clarke
@BrandonBuck: Poke found the relevant portion of the reference docs already, but that's exactly what is happening here.Doubleacting
Note: If the code is harder to understand, it's also helpful to tokenize, get the abstract syntax tree, or disassemble the code.Painterly
L
66

Whitespace between tokens

Except at the beginning of a logical line or in string literals, the whitespace characters space, tab and formfeed can be used interchangeably to separate tokens. Whitespace is needed between two tokens only if their concatenation could otherwise be interpreted as a different token (e.g., ab is one token, but a b is two tokens).

So in this case, 1if is not a valid token, so the whitespace is optional. The 1 is interpreted as an integer literal of which the if is not a part. So if is interpreted separately and recognized as a keyword.

In xif however, an identifier is recognized, so Python is not able to see that you wanted to do x if there.

Lucarne answered 2/6, 2014 at 15:27 Comment(6)
By that reasoning, shouldn't 1 if 1else 0 parse? (it doesn't)Earley
@Earley I was confused for a moment and thought it should parse as well, but 1e is actually interpreted as the start of a literal in scientific notation; e.g. 1e3 == 1000Seleucid
Interesting! After reading your comment, I have noticed that 1 if 1jelse 0 does parse.Earley
@wim: or 1 if 0b1else 0. The fact that 1else doesn't parse shows that the rule cited in this answer is not entirely consistent with the implementation, because 1else could not otherwise be interpreted as a different token, and neither can 1e. (0x1else doesn't parse either, but that's because the maximal munch rule makes it into 0x1e lse, both of which are valid.)Auteur
I'm pretty sure this is a bug. I'll make an issue on the tracker.Maracanda
That bug was now fixed, so it should work in every next release.Lucarne
I
4

The Python lexer generates two tokens for the input 1if: the integer 1 and the keyword if, since no token that begins with a digit can contain the string if. xif, on the other hand, is recognized as a valid identifier; there is no reason to believe that it is an identifier followed by a keyword, and so is passed to the parser as a single token.

Iconography answered 2/6, 2014 at 15:27 Comment(0)
C
3

With my limited knowledge of lexical processing and tokenizing I'd say what you're seeing is that any piece that can be lexical parsed as "different" (i.e. numbers/dictionaries, etc...) from the if are being done so. Most languages ignore spaces and I imagine that Python does the same (excluding, of course indentation levels). Once tokens are generated the grammar itself doesn't care, it most likely looks for an [EXPRESSION] [IF] [EXPRESSION] [ELSE] [EXPRESSION] grouping, which, again with your examples, would work fine.

Clarke answered 2/6, 2014 at 15:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.