2

In a python script I saw something like the following:

someobject.name = name = otherobject.getname()

Since I don't know how this is called, it's difficult to google it, although I think it is trivial if you know the name for that expression.

Can someone tell me what this does? My idea is that first

name = otherobject.getname()

is called, and then

someobject.name = name

But I am not sure if it works that way, and if the order of execution is like that.

Raimund Krämer
  • 1,255
  • 1
  • 11
  • 29
  • a short answer: you're right. – Remi Guan Sep 21 '15 at 12:54
  • The proper term is "multiple assignments". This was the 2nd, but the [first hit on Google](http://stackoverflow.com/q/5182573/2564301) may also be interesting for you. – Jongware Sep 21 '15 at 12:54
  • 2
    The [Python *assignment statements* documentation](https://docs.python.org/2/reference/simple_stmts.html#assignment-statements) can also be enlightening. *An assignment statement evaluates the expression list (remember that this can be a single expression or a comma-separated list, the latter yielding a tuple) and assigns the single resulting object to each of the target lists, from left to right.* – Martijn Pieters Sep 21 '15 at 12:54
  • 1
    @KevinGuan: actually, no, they are not right. Assignment takes place from left to right after the right-hand-side expression has been evaluated. So `temp = otherobject.getname()`, then `someobject.name = temp` followed by `name = temp`. – Martijn Pieters Sep 21 '15 at 12:55
  • @MartijnPieters Really? I thought that is from right to left. Thanks for explanation. :) – Remi Guan Sep 21 '15 at 12:58
  • 2
    @KevinGuan: see [Python Assignment Operator Precedence - (a, b) = a\[b\] = {}, 5](http://stackoverflow.com/q/32127908) for a little brain teaser that relies on that order of operations. Or [Issue with Python implementation of assignment chaining](http://stackoverflow.com/q/16045254) for how misunderstanding the assignment order can lead to interesting bugs. – Martijn Pieters Sep 21 '15 at 13:41

0 Answers0