You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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` statementif (a, b:=0, 0):
print(a, b)
The result is:
233 0
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 :=.
The text was updated successfully, but these errors were encountered:
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
a
is never assigned, because of the syntax of:=
operator:where
NAME
is a valid identifier andexpr
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:=
.The text was updated successfully, but these errors were encountered: