3 ms·
Trying to clarify it a bit, it gets interpreted as: "[0xf or (x in (1,2,3))]" and the "or" short circuits, never evaluating the second part (which would have gi
by jVinc 5y ago
Trying to clarify it a bit, it gets interpreted as: "[0xf or (x in (1,2,3))]" and the "or" short circuits, never evaluating the second part (which would have given an uninitialized variable error). This therefore evaluates to [0xf] or just [15] once you convert the hex notation to an integer.
If you neglect the outer list that's only there to fool you into thinking this has something to do with list comprehensions, same as the x, and if you don't use hex to make it seem like there is a "for" in the middle, it boils down to something like: "15or whatever()" which doesn't seem all that confusing, even if "whatever" uses an uninitialized variable like x, or is undefined, because we're in Python and it's only evaluated when it runs. Then we are left with the 'confusion' boiling down to a Wat about why it's legal to do "15or 3" without space before or, and especially why "x0for 3" works. This is documented as other comments mention and is due to the way the parsing works.
I don't know if the parser could be changed to require a leading space before the or operator, but it's pretty clear to me that this is only confusing if you intentionally do you very best to try to add confusing structure around it in an attempt to fool the reader into thinking something very different and weird is going on.
- takeda 5y agoYeah, this also evaluates to [15] and is easier to understand what's going on: [15or x in (1, 2, 3)] As another person pointed out, Python's lexer sees 0xfor (or 15or) and splits it into "0xf", "or" ("15", "or" for the other case) then parser processes it as usual.
- d23 5y ago> it's pretty clear to me that this is only confusing if you intentionally do you very best to try to add confusing structure around it in an attempt to fool the reader into thinking something very different and weird is going on. Sure, but that's half the fun :-) I frequently see Ned's name pop up with interesting things like this.
- sco1 5y ago> it's pretty clear to me that this is only confusing if you intentionally do you very best to try to add confusing structure around it in an attempt to fool the reader into thinking something very different and weird is going on. This has come up from a Core Developer before [1] so it's not just code golfers having a laugh. Yes, the docs note the tokenization behavior [2], but Guido's response today in the above mail thread is also pretty unambiguous: > I would totally make that a SyntaxError, and backwards compatibility be damned. 1: https://mail.python.org/archives/list/python-dev@python.org/thread/D2WPCITHG2LBQAP7DBTC6CY26WQUBAKP/#D2WPCITHG2LBQAP7DBTC6CY26WQUBAKP https://mail.python.org/archives/list/python-dev@python.org/... 2: https://docs.python.org/3/reference/lexical_analysis.html#whitespace-between-tokens https://docs.python.org/3/reference/lexical_analysis.html#wh...