This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Error should be flagged if Walrus operator is used for multiple assigment
Type: compile error Stage: resolved
Components: Versions: Python 3.9
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: BTaskaya, JohnPie
Priority: normal Keywords:

Created on 2020-11-09 10:48 by JohnPie, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
walrustest.py JohnPie, 2020-11-09 10:48 Python code
Messages (2)
msg380580 - (view) Author: John Zalewski (JohnPie) Date: 2020-11-09 10:48
#The 'Walrus' operator does not support multiple assignment but does not #flag an attempt to make a multiple assigment as an error
#This results in unexpected behavior at execution time:

a, b = 100, 200

print (a, b)
#result is
#100 200

if (a, b := 3, 4): #this should be flagged as an error but is not



  print ("found true")
else:
  print ("found false")

print (a, b)
#result is
#100 3 but if multiple assigment were allowed this would be '3, 4'


----------------------------------------------------------------------
Python 3.9.0 (tags/v3.9.0:9cf6752, Oct  5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> 
= RESTART: C:\Users\John PC 2017\AppData\Local\Programs\Python\Python38-32\walrustest.py
100 200
found true
100 3
>>>
msg380581 - (view) Author: Batuhan Taskaya (BTaskaya) * (Python committer) Date: 2020-11-09 10:54
Due to the precedence of the walrus operator, it is not actually a multiple assignment but rather a tuple of 3 elements with one being the value of the assignment expression.

 $ python -m ast  
(a, b := 3, 4)
Module(
   body=[
      Expr(
         value=Tuple(
            elts=[
               Name(id='a', ctx=Load()),
               NamedExpr(
                  target=Name(id='b', ctx=Store()),
                  value=Constant(value=3)),
               Constant(value=4)],
            ctx=Load()))],
   type_ignores=[])

In this case, it creates a tuple with loading the name `a` from the current scope, using the value of the 3 and also assigning 3 to the b, and loading constant 4.

So basically (a, b := 3, 4) is actually (a, (b := 3), 4)
History
Date User Action Args
2022-04-11 14:59:37adminsetgithub: 86461
2020-11-09 12:44:05BTaskayasetstatus: open -> closed
resolution: not a bug
stage: resolved
2020-11-09 10:54:43BTaskayasetnosy: + BTaskaya
messages: + msg380581
2020-11-09 10:48:14JohnPiecreate