Hello.
I found that the syntax of :=, the walrus operator as a new feature in Python 3.8, is kind of confusing and newbies may make mistakes on it.
Consider the following example:
a, b = 233, 233
# Try assigning to `a`, `b` in `if` statement
if (a, b := 0, 0):
print(a, b)
The result is:
You can see that a is never assigned, because of the syntax of := operator:
NAME := expr
where NAME is a valid identifier and expr is a valid expression. Thus unpack assignment is not supported. And code (a, b := 0, 0) is just the same as (a, 0, 0).
You must choose either unpack assignment with normal = or the walrus operator :=.
Hello.
I found that the syntax of
:=, the walrus operator as a new feature in Python 3.8, is kind of confusing and newbies may make mistakes on it.Consider the following example:
The result is:
You can see that
ais never assigned, because of the syntax of:=operator:where
NAMEis a valid identifier andexpris a valid expression. Thus unpack assignment is not supported. And code(a, b := 0, 0)is just the same as(a, 0, 0).You must choose either unpack assignment with normal
=or the walrus operator:=.