How does Python's comma operator works during assignment?

后端 未结 3 1218
栀梦
栀梦 2020-11-29 05:58

I was reading the assignment statements in the Python docs ( http://docs.python.org/reference/simple_stmts.html#assignment-statements ).

In that it is quoted that:

3条回答
  •  伪装坚强ぢ
    2020-11-29 06:37

    Python does not have a "comma operator" as in C. Instead, the comma indicates that a tuple should be constructed. The right-hand side of

    a, b = a + b, a
    

    is a tuple with th two items a + b and a.

    On the left-hand side of an assignment, the comma indicates that sequence unpacking should be performed according to the rules you quoted: a will be assigned the first element of the tuple, b the second.

提交回复
热议问题