Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Example: Confusing syntax of walrus operator := #145

Closed
ipid opened this issue Oct 23, 2019 · 0 comments
Closed

Example: Confusing syntax of walrus operator := #145

ipid opened this issue Oct 23, 2019 · 0 comments
Assignees
Milestone

Comments

@ipid
Copy link

ipid commented Oct 23, 2019

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:

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 :=.

@satwikkansal satwikkansal self-assigned this Oct 29, 2019
@satwikkansal satwikkansal added this to the 3.0 milestone Oct 29, 2019
muscliary pushed a commit to muscliary/wtfpython that referenced this issue Sep 12, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

2 participants