> list object is constructed once and assigned to both variables
Ummm no the list is constructed once and assigned to b and then b is assigned to a. It would be crazy semantics if `a = b = ...` meant `a` was assigned `...`.
ayhanfuat•40m ago
> then b is assigned to a
Wouldn't that require a LOAD_FAST? Also a is assigned first (from left to right) so a = ... happens either way.
kccqzy•20m ago
It’s assigned left to right, not right to left. It’s documented in the Python language reference.
> An assignment statement evaluates the expression list and assigns the single resulting object to each of the target lists, from left to right.
Consider this:
a = [1, 2]
i = a[i] = 1
If assignment were to happen right to left, you would get a NameError exception because the first assignment would require an unbound variable.
kccqzy•49m ago
Chained assignments are banned according to the style guide at my workplace. Too many opportunities for misuse. And if you insist on a one-liner assignment to two variables just use two statements separated by the semicolon. I challenge anyone to work out what this code does:
mathisfun123•1h ago
Ummm no the list is constructed once and assigned to b and then b is assigned to a. It would be crazy semantics if `a = b = ...` meant `a` was assigned `...`.
ayhanfuat•40m ago
Wouldn't that require a LOAD_FAST? Also a is assigned first (from left to right) so a = ... happens either way.
kccqzy•20m ago
> An assignment statement evaluates the expression list and assigns the single resulting object to each of the target lists, from left to right.
Consider this:
If assignment were to happen right to left, you would get a NameError exception because the first assignment would require an unbound variable.