If python assignments don't return a value, how can we do a = b = c = 42

Having looked at these two questions,

I have a question:

How does it a = b = c = 42spread to the 42left without returning a value at every step?

+4
source share
3 answers

Due to a special exception in the syntax cut out for this exact use case. See BNF:

assignment_stmt ::=  (target_list "=")+ (expression_list | yield_expression)

Pay attention to (target_list "=")+.

+8
source

. , .

+3

This is a python function. From Python Tutorial :

A value can be assigned to several variables at the same time:

>>> x = y = z = 0  # Zero x, y and z
>>> x
0
>>> y
0
>>> z
0

Note that assignment does not actually return any value. You cannot do it

a = b = (c = 2)
+1
source

Source: https://habr.com/ru/post/1533028/


All Articles