"There is new syntax := that assigns values to variables as part of a larger expression," the Python 3.8.0 release notes state.
"It is affectionately known as 'the walrus operator' due to its resemblance to the eyes and tusks of a walrus."
In this example, the assignment expression helps avoid calling len() twice:
if (n := len(a)) > 10:
print(f"List is too long ({n} elements, expected <= 10)")
No comments:
Post a Comment