For more information on concepts covered in this lesson, you can check out:
Assignment Expression Syntax
Walrus operator syntax. One of the main reasons assignments were not expressions in Python from the beginning is the visual likeness of the assignment operator (
=) and the equality comparison operator (
==). This could potentially lead to bugs.
00:18 When introducing assignment expressions, a great deal of thought was put into how to avoid similar bugs with the walrus operator. As mentioned previously, one important feature is the walrus operator is never allowed as a direct replacement for the assignment operator, and vice versa.
00:35 As you saw at the beginning of the course, you can’t use a plain assignment expression to assign a value. It is syntactically legal to use an assignment expression to only assign a value, but only if you add parentheses.
01:40 In all of these cases, you would be better served using the assignment operator instead. The next examples are all similar and they are all legal code. However, the walrus operator doesn’t improve your code in any of these cases.
02:20 None of these examples make your code more readable. You should instead do the extra assignment separately by using a traditional assignment statement. If you want to look at this in more depth, take a look at the original PEP which created the assignment expression operator.
:= in this case does look like a walrus operator, but the effect is quite different. To interpret
x:=8 inside the f-string, the expression is broken into three parts: the
x, the colon (
:), and the
x is a value, the colon (
:) acts as a separator, and
=8 is a format specification.
05:14 The scope of the assignment target is the same as for assignments. It will follow the LEGB rule. Typically, the assignment will happen in the local scope, but if the target name is already declared global or non-local, that is honored.
05:33 For more information on this topic, check out this Real Python course, Python Scope & the LEGB Rule: Resolving Names in Your Code. The precedence of the walrus operator can cause some confusion.
05:47 It binds less tightly than all other operators except the comma, so you might need parentheses to delimit the expression that’s assigned. As an example, note what happens when you don’t use parentheses.
square is bound to the whole expression
number ** 2 > 5. In other words,
square gets the value
True and not the value of
number ** 2, which was the intention. In this case, you can delimit the expression with parentheses.
The parentheses make the
if statement both clearer and actually correct. There’s one final gotcha. When assigning a tuple using the walrus operator, you always need to use parentheses around the tuple.
07:33 This may seem a bit annoying. However, if the walrus operator bound less tightly than the comma, it wouldn’t be possible to use the walrus operator in function calls with more than one argument.
07:57 Second, use parentheses around the expression as necessary but avoid adding extra parentheses that are not needed. The general design of assignment expressions is to make them easy to use when they’re helpful but to avoid overusing them when they might clutter up your code.
Become a Member to join the conversation.