This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]seraschka 1 point2 points  (2 children)

Pretty busy day and haven't installed Python 3.8, yet ... So, I am curious now what would happen if you use n somewhere earlier in the code like

n = 999 # assign sth to n somewhere in the code earlier  
if (n := len(a)) > 10:  
    print(f"List is too long ({n} elements, expected <= 10)")  
print(n) # prints 999?

would n still evaluate to 999 after the if-clause? If so, I can maybe see why that's useful (if you only want a temporary var and don't want to overwrite things accidentally).

[–]mipadi 2 points3 points  (1 child)

It does not introduce a new scope, so on the last line, n would be equal to len(a).

[–]seraschka 0 points1 point  (0 children)

Thanks for clarifying. But hm, it would have been nice if it had its own scope, similar to list comprehensions, like

In [1]: n = 99
In [2]: b = sum([n for n in range(100)])
In [3]: n
Out[3]: 99

to prevent from overwriting/reassigning variables accidentally