1277120515
1277120515

Reputation: 21

who can explain `foo, bar = foo[bar] = [1,2,3], 2`? (python)

I cannot understand the code as follows:

>>> foo, bar = foo[bar] = [1,2,3], 2
>>> foo
>>> [1, 2, ([...], 2)]
>>> bar
>>> 2

In my opinion,

>>> foo, bar = foo[bar] = [1,2,3], 2

is a multiply assignments, which can be written as:

>>> foo[bar] = [1,2,3], 2
>>> foo, bar = foo[bar]

obviously, the 1st assignment will cause error.

However, the foo, bar = foo[bar] = [1,2,3], 2 is amazingly legal, and show me a strange result.

who can explain why?

I expecting that someone can explain to me.

Upvotes: 0

Views: 40

Answers (0)

Related Questions