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?
if
is a new token. – Doubleacting