The := walrus operator is not easy to teach. Should you cover it at all?
I used to worry about a participant getting stuck on some Python syntax because I didn’t cover it during a course. But these days between the internet, search engines and AI it is very easy for someone to fill in any gaps.
The walrus operator is a part of Python which, in very specific circumstances, can let you write slightly more elegant code. It also, in many other circumstances, lets you write less readable code. For a while I had been wondering whether I should teach it at all.
Listening to a conversation between Trey Hunter and the hosts of the Teaching Python podcast recently confirmed my thoughts. As Trey put it, when talking about the walrus operators, “it adds an extra complication and extra mental overhead for someone who’s just learning”.
In short, the walrus operator is an optional extra which I only cover when there is plenty of time.
If you think that short code is good code, you’ll find plenty of uses for the walrus operator. That seems to be the justification for many of the examples I see online. Writing short code is fine - for fun - but not for any serious code. For me clarity trumps brevity, especially in production code. Any code which matters, which is meant to be used in anger, which is meant to last a long time, should be clear, easy to read and simple to understand.
For instance, I don’t like how the walrus operator is used here
name = 'Susie'
if (number_of_characters := len(name)) > 4:
print(f'There are {number_of_characters} characters in your name')
else:
print('That is quite a short name')
This is slightly longer, but more readable
name = 'Susie'
number_of_characters = len(name)
if number_of_characters > 4:
print(f'There are {number_of_characters} characters in your name')
else:
print('That is quite a short name')
This short but difficult to read code …
result = "big" if (number_of_characters := len("hello")) > 4 else "small"
print(number_of_characters, result)
… would be better written like this
number_of_characters = len("hello")
result = "big" if number_of_characters > 4 else "small"
print(number_of_characters, result)
When I do cover it
Here is how I cover the walrus operator, if I cover it
I’ll show them an example like this
reply = input('Quit (y/n)? ')
while reply not in ['y', 'n']:
reply = input('Quit (y/n)? ')
print(reply)
And I ask them: “What do you think I don’t like about this”
Hopefully someone will point out the repetition and I remind them of the DRY (Don’t Repeat Yourself) principle. Next I will ask them how they would fix this.
Based on their replies we may try a few different versions
reply = 'x'
while reply not in ['y', 'n']:
reply = input('Quit (y/n)? ')
print(reply)
“This version is not bad. It feels like we’re using a bit of trick to work around the problem, but it will work”
while input('Quit (y/n)? ') not in ['y', 'n']:
pass
print(reply)
“The loop itself works fine, but we are no longer storing the reply, so we can’t print it or otherwise use it once we’re out of the loop”
“The problem is that we want to use the reply directly - in the ‘while’ statement, but we also want to store it for later”
“Normally we either use the result from an expression directly, for instance by printing it”
print(input(' >'))
“Or by comparing it”
if input('> ') != 'quit':
print("You don't want to quit. Okay, let's continue")
“Or, we can store it for later use, by giving it a name and the equals sign”
reply = input('> ')
if reply != 'quit':
print(f"Your reply was '{reply}'. Let's continue")
“So, without the walrus operator we either use the result of an expression directly, or we store it for later”.
“Using the walrus operator we can do both at the same time - use it and store it”
while reply := input('Quit (y/n)? ') not in ['y', 'n']:
pass
print(reply)
“In the first line we ask for a reply. We then check whether it is either ‘y’ or ’n’. And we store it for later. We use and we store it”
Finally, I may also show them some examples how not to use it.
At Python trainer we need to judge what we cover and don’t cover in a course. The walrus operator should be treated as optional Python syntax. And, if you do cover it, show when it is appropriate to use it.