Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

After looking at these two questions,

  • Why does Python assignment not return a value?
  • Python assigning multiple variables to same list value?

I had this question:

How does a = b = c = 42 propagate 42 to the left without returning the value at each step?

like image 301
sgarg Avatar asked Dec 15 '22 00:12

sgarg


2 Answers

Because of a special exception in the syntax, carved out for that exact use case. See the BNF:

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

Note the (target_list "=")+.

like image 173
icktoofay Avatar answered Dec 22 '22 00:12

icktoofay


Chained assignment in this manner does not require assignment to return a value. It is a special form of the assignment statement which binds the object to multiple names.

like image 36
Ignacio Vazquez-Abrams Avatar answered Dec 21 '22 23:12

Ignacio Vazquez-Abrams