Submind YouTube summaries
Thumbnail for The most controversial Python feature | Walrus operator

The most controversial Python feature | Walrus operator

Watch on YouTube

Video summary

The Walrus operator, formally known as the assignment expression or `:=`, was introduced to Python 3.8 following a contentious period involving significant drama and toxicity within the community. This feature enabled developers to combine an assignment operation with its return value into a single statement, diverging from traditional notation where the equals sign denotes equality rather than assignment—a convention largely attributed to Fortran's influence on early programming languages. While some languages like Pascal use similar symbols for assignment, Python initially resisted this concept until it was accepted as part of PEP 572. The controversy surrounding its inclusion is notable enough that it prompted Guido van Rossum, the creator and long-time leader of Python known as the Benevolent Dictator for Life (BDFL), to step down from his leadership role after accepting the proposal. The primary utility of the Walrus operator lies in condensing complex logic into more concise lines of code, particularly in scenarios involving regular expressions and file reading. For instance, when using regex matching, developers can now assign the result of a match operation directly within an `if` statement to check for success without needing separate variables or intermediate steps. Similarly, it simplifies file processing loops by allowing the read operation and the condition checking whether data was fetched or if the end-of-file was reached to occur in one line. Beyond these specific cases, the operator allows list comprehensions to reuse expensive computations efficiently; a function can be called once, assigned to a temporary variable using `:=`, and then referenced multiple times within the same expression without recalculating it. Despite its elegance when used correctly, the feature faced substantial criticism regarding programming language design principles outlined in PEP 20, or "The Zen of Python." Critics argued that violating the principle that there should be only one obvious way to do something was problematic, noting that C's use of `=` for assignment expressions had historically led to errors. Furthermore, opponents claimed that while the operator reduced whitespace visually, it actually increased logical complexity and violated the tenet that simple is better than complex. A major concern in programming language design generally—and specifically here—is the unpredictability of how thousands of developers will utilize new features once they are released into production environments, a challenge that was not fully addressed during the development phase due to insufficient testing on real-world usage patterns. Ultimately, the Walrus operator serves as more than just a syntactic convenience; it symbolizes the critical role of leadership in navigating community disagreements and breaking through toxic stalemates. Guido van Rossum's decision to resign from his position highlighted how intense internal conflict can paralyze progress, leading him to state that he no longer wished to fight so hard for proposals only to face such widespread opposition. His departure underscores a broader lesson about the necessity of leaders who are willing to make difficult and sometimes unpopular decisions to advance technology forward. In this context, the feature stands as a reminder that while community consensus is valuable, decisive leadership is often required to overcome divisiveness and ensure that software development continues to evolve despite chaos and disagreement.
Read the full video 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 what 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 context 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 rest 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 chunk contains anything or if 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 fetch 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 the 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 f1 assigning it to attempt 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 aligned stand alone by themselves without using parentheses i think this clarifies to beginners that assignment expression should not be used as an assignment statement there's a set of principles defined in pap 20 otherwise called zen 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 first principle that's been brought up is there should be only one obvious way to do it and some people argue 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 and see 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 then a 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 operator is 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 engineer 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 uh 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 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