Created
November 7, 2022 14:38
-
-
Save tmr232/d8df7dbbaf48265a8ac19c1a8cffb762 to your computer and use it in GitHub Desktop.
Differences in order of evaluation between Java and Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is actually a JShell script, because I am lazy | |
int c = 0; | |
int nxt() { | |
c += 1; | |
return c; | |
} | |
int l[] = {1, 2, 3, 4}; | |
l[nxt()] = l[nxt()]; | |
// The LHS is evaluated before the RHS. | |
// See https://docs.oracle.com/javase/specs/jls/se19/html/jls-15.html#jls-15.7 | |
// l == {1, 3, 3, 4} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
c = 0 | |
def nxt(): | |
global c | |
c += 1 | |
return c | |
l = [1, 2, 3, 4] | |
l[nxt()] = l[nxt()] | |
# The RHS is evaluated before the LHS. | |
# See https://docs.python.org/3/reference/expressions.html#evaluation-order | |
# l == [1, 2, 2, 4] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment