Kind: captions Language: en 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 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