0xf = Hex for 15
Expression is evaluated as a boolean
0xf = Hex for 15
Expression is evaluated as a boolean
>>> ast.dump(ast.parse('[0xfor x in (1,2,3)]'))
"Module(body=[Expr(value=List(elts=[BoolOp(op=Or(), values=[Num(n=15), Compare(left=Name(id='x', ctx=Load()), ops=[In()], comparators=[Tuple(elts=[Num(n=1), Num(n=2), Num(n=3)], ctx=Load())])])], ctx=Load()))])"* 0xfor1 evaluates.
* 1or 2 evaluates.
* 1or2 doesn't.
* ''or'foo' evaluates.
This is gross.
“1or2” is lexed into “1” (integer) followed by “or2” (identifier), which is valid on the lexer level but then fails on the grammar level.
I guess it’s an operator token after all.
https://docs.python.org/3/reference/lexical_analysis.html#wh...
This is not a bug.
> 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).
The two tokens in this case are "0xf" and "or". Their concatenation cannot be interpreted as a different token, because "0xfor" is not a valid token. Therefore, if I'm reading the rule correctly, whitespace is needed in this case.
"0xffor" is another interesting case. It's also not a valid token, but it could be interpreted as two tokens in two different ways: "0xf" "for" or "0xff" "or". (Python does the latter. I presume it uses something like C's "maximal munch" rule.)
> 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).
Because the concatenation of "0xf" and "or" can't be interpreted as a different token, the whitespace is not needed.
I dislike the rule, and I strongly think that "0xfor" should require whitespace between "0xf" and "or" (I'm sure that influenced my reading), but you're right about what the rule says.
(Apparently I can't edit my previous comment.)
But requiring whitespace between all tokens is not an acceptable solution, since "2+2" should work. Always equiring whitespace between alphanumerical characters in different tokens would make sense.
edit: 'cause it's a bug! https://bugs.python.org/issue43833
The question is whether "0xfor" is a valid way to represent the two tokens "0xf", "or". My reading of the rules is that it isn't, and that whitespace is required.
The rule, which I initially misinterpreted, is:
> 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).
Since "0xfor" cannot be interpreted as a different (single) token, whitespace is not needed.
(I'm not a fan of this particular consequence of that rule, and I suspect it was not intended, but it says what it says.)
[sielicki@dogfruit ~]$ python3.10 -c 'print(0xf or some_undefined_variable in some_undefined_set)'
15This code "should" result in a NameError or something similar. But resolving that identifier to a name won't happen until we decide to evaluate the right-hand side of the `or`.
Thankfully, most python static checkers make some mostly-sane assumptions and will flag this code as an error, despite the subtle possibility of legally creating this name using some other Python/CPython features.
Type annotations are a great idea but in practice I don't see them used much. Should be great for dev teams working on bigger Python projects.
Whitespace in Python is significant to indicate statements and block.
I still can't figure out what the author was trying to do when he stumbled onto that, though...
That is, 0or, 0xor, 0and,etc. in my view, this should clearly be a syntax error.
edit: as noted by the parent comment, 0xor != 0 xor, but 0x or. But this seems to hold true for all data types.
I can see how this could slip by, it's possible that original python used a non-alphabetical token as the logical operator, then it was swapped out at some later time for or and and.
[0x1for x in (1,2,3)]
looks like it should parse as [0x1 for x in (1,2,3)]
but instead parses as [0x1f or x in (1,2,3)]