How does Python's comma operator works during assignment?

后端 未结 3 1219
栀梦
栀梦 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:14

    All the expressions to the right of the assignment operator are evaluated before any of the assignments are made.

    From the Python tutorial: First steps towards programming:

    The first line contains a multiple assignment: the variables a and b simultaneously get the new values 0 and 1. On the last line this is used again, demonstrating that the expressions on the right-hand side are all evaluated first before any of the assignments take place. The right-hand side expressions are evaluated from the left to the right.

    Emphasis mine.

    Your code is functionally equivalent to the following:

    a, b = 5 + 4, 5
    print a, b
    

提交回复
热议问题