+ 1

Weird Python Weirdness

I made a code demonstrating and explaining the problem here: https://code.sololearn.com/c0BU1ctxAZG4/?ref=app

28th Dec 2020, 8:01 PM
Jason Stone
Jason Stone - avatar
4 Antworten
+ 10
I believe 1 == "hj" in "ahj" is internally changed to 1 == "hj" and "hj" in "ahj" which in turn evaluates to False and True which is False. By introducing parentheses, this change isn't done so it creates different results.
28th Dec 2020, 8:33 PM
Russ
Russ - avatar
+ 7
This is the official docs. Try googling "comparison chaining" or similar if you'd like more info. https://docs.python.org/3.9/reference/expressions.html#comparisons
28th Dec 2020, 9:09 PM
Russ
Russ - avatar
+ 1
Russ ah, I see. Thank you.
28th Dec 2020, 9:32 PM
Jason Stone
Jason Stone - avatar
0
Russ That’s interesting. Is there any documentation anywhere on this internal changing?
28th Dec 2020, 8:58 PM
Jason Stone
Jason Stone - avatar