What is this kind of assignment in Python called? a = b = True

前端 未结 4 874
有刺的猬
有刺的猬 2020-11-29 04:20

I know about tuple unpacking but what is this assignment called where you have multiple equals signs on a single line? a la a = b = True

It always trips

4条回答
  •  抹茶落季
    2020-11-29 05:11

    It's a chain of assignments and the term used to describe it is...

    - Could I get a drumroll please?

    Chained Assignment.


    I just gave it a quite google run and found that there isn't that much to read on the topic, probably since most people find it very straight-forward to use (and only the true geeks would like to know more about the topic).


    In the previous expression the order of evaluation can be viewed as starting at the right-most = and then working towards the left, which would be equivalent of writing:

    b = True
    a = b
    

    The above order is what most language describe an assignment-chain, but python does it differently. In python the expression is evaluated as this below equivalent, though it won't result in any other result than what is previously described.

    temporary_expr_result = True
    
    a = temporary_expr_result
    b = temporary_expr_result
    

    Further reading available here on stackoverflow:

    • How do chained assignments work? python

提交回复
热议问题