Skip to content

Instantly share code, notes, and snippets.

@tmr232
Created November 7, 2022 14:38
Show Gist options
  • Save tmr232/d8df7dbbaf48265a8ac19c1a8cffb762 to your computer and use it in GitHub Desktop.
Save tmr232/d8df7dbbaf48265a8ac19c1a8cffb762 to your computer and use it in GitHub Desktop.
Differences in order of evaluation between Java and Python
// 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}
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