Skip to content

The order of execution #16292

Nov 24, 2024 · 2 comments · 1 reply
Discussion options

You must be logged in to vote

Yes. It's usually wise to put assignment expressions in parens. The following works as expected:

>>> r = 0
>>> if (r:= 1) and r==1: print(f"r = {r}")
... 
r = 1

But this does not:

>>> r = 0
>>> if r:= 1 and r==1: print(f"r = {r}")
... 
>>> r
False
>>> 

because the latter is effectively doing

if r:= (1 and r==1): print(f"r = {r}

which sets r False (because the comparison returns False).

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@krismc
Comment options

Answer selected by krismc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants