Back to Index

The most controversial Python feature | Walrus operator


Chapters

0:0 Walrus operator
1:26 Use cases
3:33 Criticisms
5:59 Guido van Rossum

Transcript

This is the walrus operator, and this is the assignment expression that it enables. It's been added as a feature to Python 3.8 after a whole lot of drama. Majority of Python core developers, as far as I can tell, were against it, and the drama and toxicity over it drove Guido van Rossum, creator of Python, to step down from his leadership role of benevolent dictator for life after he accepted the walrus operator as part of the PEP 572 proposal.

So what is it? First, let's talk about the assignment statement, the equal sign in Python. We can thank Fortran for this, I think, not very good design decision of using the equal sign for assignment. So assigning, in this case, 42 to the variable x. Most programming languages use it, with a few exceptions.

Pascal uses the walrus operator for assignment, and R uses a different operator. This goes against the notation of mathematics, where the equal sign is used to designate equality. But there's a lesson here, I think, that even bad design decisions, once accepted and once we get used to them, aren't so bad.

It's all about consistency. So the assignment expression does a little bit more. It also signs 42 to x, performs the assignment operation, but as an expression, it returns the value that it assigned as well. This can be useful in a lot of contexts. Let me talk about the cases where I find them particularly useful.

So I use regular expressions a lot as part of data science to clean up and organize data. So a common piece of code you might see is you perform a regular expression match, returning it to a variable like res here, that contains the regular expression object if a match was found, and contains none if no match was found.

And then you have some kind of conditional, like an if statement, that checks whether a match was found. And if it has been found, then you do something with the regular expression object. In Python 3.8, all that gets combined into a single line that performs the match operation, assigns the result of the match to res, and then check if res contains a regular expression object.

A similar use case is in reading files. There's a lot of ways to read a file, but a lower level version, there's usually a while loop. Then there's a read operation that's stored in the chunk variable or something like that. And then there's a condition that checks whether the chunk contains anything, or if an end of file was reached, at which point you break out of the while loop.

If it has not been reached, then you do something with the data that was fetched. In Python 3.8, this gets compressed into a single line that performs the read operation, the assignment of the fetched data to the variable chunk, and then the conditional check in the while loop of whether chunk returned any data or if the end of the file was reached.

To me, this is elegant. Other use cases in list comprehensions, you can share sub-expressions. Like in the filter, you can assign f of x to y, and then reuse that y in the output of the comprehension. You can also, in a list, reuse a value. For example, if it's expensive to compute, so you can compute f of x once, assign it to y, and then reuse y in the rest of the list definition.

As a side note, I don't like to use chained assignments, but chain assignments have the same feature that the function f is only computed once. So this particular statement here is equivalent to computing f once, assigning it to a temp variable, and then assigning that temp variable to both x and y.

There are quite a few criticisms that are both objective and subjective that talk about the complexities of programming language design and human nature in general. So first is the idea that both the equal sign and the walrus operator perform an assignment, so it might be confusing to beginners. I'm not sure about the history of PEP 572, but my guess is that this criticism was more prevalent before the exception was added.

That assignment expressions can't be on a line standalone by themselves without using parentheses. I think this clarifies to beginners that assignment expressions should not be used as an assignment statement. There's a set of principles defined in PEP 20, otherwise called "Design of Python" that has some ideas to aspire to in the design of Python.

Just like Guido said, some of these are subjective, and I think they are ideas to aspire to as opposed to perfectly implement because they're overlapping and there's a natural tension between them. So the first principle that's been brought up is there should be only one obvious way to do it, and some people argued that the walrus operator performs an operation that already had another way to do the same exact thing.

I've also heard a funny kind of criticism that the equals operator worked like an assignment expression in C, and it kind of sucked in C, or at least was error-prone. So there you go, you have a case study in the real world where this kind of operator being used to designate an assignment expression was error-prone.

Another "Design of Python" principle that people brought up is that simple is better than complex, and while it seems that the assignment expression makes the code simpler, in fact, it was argued that it was only reducing white space and was in fact adding complexity, or at least moving the complexity.

Finally, the criticism that applies often in programming language design is that not enough testing was done on how actual developers will use it. I think that's probably the biggest challenge of programming language design, and design in general, is you don't know how people, how thousands or millions of developers are going to use this feature once it's in the wild, and how other features will interact with it when it's in the wild.

So this criticism is certainly true, but I think it is always true. My own feelings about the walrus operators, I think when used properly, it's quite elegant and even beautiful, but it also represents more than that. To me, it represents the importance of leadership in a community of smart people that disagree.

So this is the feature that led Guido to resign, and I spoke with him on the podcast that I host, and I'll speak to him again. I think he's one of the most brilliant language designers and programmers we have, also a great leader in the software engineering community. So this is a little excerpt from a post he made.

It reads, "Now that PEP 572 is done, I don't ever want to have to fight so hard for a PEP and find that so many people despise my decisions. I would like to remove myself entirely from the decision process. I'm basically giving myself a permanent vacation from being BDFL, and you all will be on your own.

So what are you all going to do? Create a democracy? Anarchy? A dictatorship? A federation?" So to me, the walrus operator represents more than just assignment expressions. It represents the power of leadership to break through a toxic stalemate. I think leaders have to make difficult decisions, sometimes unpopular decisions, and sometimes ones, if you look at the long arc of history, prove out to be bad decisions.

But without leadership, I think we can't make progress. So the messiness, the chaos of democracy, is that the divisiveness can be paralyzing. And we need leaders to inspire us, to guide us, and to make difficult, risky decisions. So to me, the walrus operator would be useful for regular expressions, but it will also be a reminder of the importance of leadership in the programming world and in our world in general.

If you enjoy these short little videos, subscribe, and remember, try to learn something new every day. you you you you you you you