Python assignment order behaves differently than I expected. In javascript I could write this:
x = {};
a = x;
a = a['y'] = {};
console.log(a);
// {}
console.log(x)
// {'y':{}}
Because assignment happens right to left, in a = a['y'] = {};
, a['y']
gets assigned {}
, then a
gets assigned a['y']
- which is {}
;
However, in python this is not the case. The same setup:
x = {}
a = x
a = a["y"] = {}
print(a)
# {"y": {...}}
print(x)
# {}
In python, this makes a
a self-referencing object, and doesn't set "y" on x
at all. The assignment can't be left to right, because assigning a = a["y"]
before "y" is set would throw an error. So what is python doing here and why?