{"id":24877,"date":"2019-11-25T12:50:27","date_gmt":"2019-11-25T10:50:27","guid":{"rendered":"https:\/\/www.webcodegeeks.com\/?p=24877"},"modified":"2020-06-23T15:01:09","modified_gmt":"2020-06-23T12:01:09","slug":"the-controversy-behind-the-walrus-operator-in-python","status":"publish","type":"post","link":"https:\/\/www.webcodegeeks.com\/python\/the-controversy-behind-the-walrus-operator-in-python\/","title":{"rendered":"The Controversy Behind The Walrus Operator in Python"},"content":{"rendered":"\n<p>If you haven\u2019t heard, Python 3.8 features a rather controversial new operator called the walrus operator. In this article, I\u2019ll share some of my first impressions as well as the views from all sides. Feel free to share some of your thoughts as well in the comments.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Understanding the Walrus Operator<\/h2>\n\n\n\n<p>Recently, I was browsing dev.to, and I found <a href=\"https:\/\/dev.to\/codemouse92\/python-snakebytes-the-walrus-operator-57h9\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"a really cool article by Jason McDonald (opens in a new tab)\">a really cool article by Jason McDonald<\/a> which covered a new feature in Python 3.8, the walrus operator. If you haven\u2019t seen the operator, it looks like this: <code>:=<\/code>.<\/p>\n\n\n\n<p>In this article, Jason states that the new operator \u201callows you to store and test a value in the same line.\u201d In other words, we can compress this:<\/p>\n\n\n\n<div>\n<div id=\"highlighter_215602\" class=\"syntaxhighlighter  php\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<div class=\"line number3 index2 alt2\">3<\/div>\n<div class=\"line number4 index3 alt1\">4<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"php plain\">nums = [87, 71, 58]<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"php plain\">max_range = max(nums) - min(nums)<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"php keyword\">if<\/code> <code class=\"php plain\">max_range &gt; 30:<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"php spaces\">&nbsp;&nbsp;<\/code><code class=\"php plain\"># <\/code><code class=\"php keyword\">do<\/code> <code class=\"php plain\">something<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n\n\n\n<p>Into this:<\/p>\n\n\n\n<div>\n<div id=\"highlighter_57208\" class=\"syntaxhighlighter  php\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<div class=\"line number3 index2 alt2\">3<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"php plain\">nums = [87, 71, 58]<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"php keyword\">if<\/code> <code class=\"php plain\">(max_range := max(nums) - min(nums)) &gt; 30:<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"php spaces\">&nbsp;&nbsp;<\/code><code class=\"php plain\"># <\/code><code class=\"php keyword\">do<\/code> <code class=\"php plain\">something<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n\n\n\n<p>In this example, we saved a line because we moved the assignment into the condition using the walrus operator. Specifically, <strong>the walrus operator performs assignment while also returning the stored value<\/strong>.<\/p>\n\n\n\n<p>In this case, <code>max_range<\/code> will store 29, so we can use it later. For example, we might have a few additional conditions which leverage <code>max_range<\/code>:<\/p>\n\n\n\n<div>\n<div id=\"highlighter_588618\" class=\"syntaxhighlighter  php\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<div class=\"line number3 index2 alt2\">3<\/div>\n<div class=\"line number4 index3 alt1\">4<\/div>\n<div class=\"line number5 index4 alt2\">5<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"php plain\">nums = [87, 71, 58]<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"php keyword\">if<\/code> <code class=\"php plain\">(max_range := max(nums) - min(nums)) &gt; 30:<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"php spaces\">&nbsp;&nbsp;<\/code><code class=\"php plain\"># <\/code><code class=\"php keyword\">do<\/code> <code class=\"php plain\">something<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"php plain\">elif max_range &lt; 20:<\/code><\/div>\n<div class=\"line number5 index4 alt2\"><code class=\"php spaces\">&nbsp;&nbsp;<\/code><code class=\"php plain\"># <\/code><code class=\"php keyword\">do<\/code> <code class=\"php plain\">something <\/code><code class=\"php keyword\">else<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n\n\n\n<p>Of course, if you\u2019re like me, you don\u2019t really see the advantage. That\u2019s why I decided to do some research.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">First Impressions<\/h2>\n\n\n\n<p>When I first saw this syntax, I immediately thought \u201cwow, this doesn\u2019t seem like a syntax that meshes well with the Zen of Python.\u201d In fact, after revisiting the <a href=\"https:\/\/www.python.org\/dev\/peps\/pep-0020\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"Zen of Python (opens in a new tab)\">Zen of Python<\/a>, I think there are several bullet points this new syntax misses.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Beautiful Is Better Than Ugly<\/h3>\n\n\n\n<p>While beauty is in the eye of the beholder, you have to admit that an assignment statement in the middle of an expression is kind of ugly. In the example above, I <strong>had<\/strong> to add an extra set of parentheses to make the left expression more explicit. Unfortunately, extra parentheses reduce the beauty quite a bit.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Sparse Is Better than Dense<\/h3>\n\n\n\n<p>If the intent of the walrus operator is to compress two lines into one, then that directly contradicts \u201csparse is better than dense.\u201d In the example I shared above, the first condition is fairly dense; there\u2019s a lot to unpack. Wouldn\u2019t it always make more sense to place the assignment on a separate line?<\/p>\n\n\n\n<p>If you\u2019re looking for a good example of a feature that compresses code, <a href=\"https:\/\/therenegadecoder.com\/code\/how-to-write-a-list-comprehension-in-python\/\">take a look at the list comprehension<\/a>. Not only does it reduce nesting, but it also makes the process of generating a list much simpler. To be honest, I don\u2019t get that vibe with the walrus operator. Assignment is already a pretty easy thing to do.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">In the Face of Ambiguity, Refuse the Temptation to Guess.<\/h3>\n\n\n\n<p>In the example above, I introduced parentheses to make the condition more explicit. Had I left out the parentheses, it becomes a little more difficult to parse:<\/p>\n\n\n\n<div>\n<div id=\"highlighter_335860\" class=\"syntaxhighlighter  php\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"php keyword\">if<\/code> <code class=\"php plain\">range := max(nums) - min(nums) &gt; 30:<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n\n\n\n<p>In this case, we have several operators on a single line, so it\u2019s unclear which operators take precedence. As it turns out, the arithmetic comes first. After that, the resulting integer is compared to 30. Finally, the result of that comparison (<code>False<\/code>) is stored in range and returned. Would you have guessed that when looking at this line?<\/p>\n\n\n\n<p>To make matters worse, the walrus operator makes assignment ambiguous. In short, the walrus operator looks like a statement, but it behaves like an expression with side effects. If you\u2019re unsure why that might be an issue, check out my article on <a href=\"https:\/\/therenegadecoder.com\/code\/the-difference-between-statements-and-expressions\/\">the difference between statements and expressions<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">There Should Be One\u2014and Preferably Only One\u2014Obvious Way to Do It.<\/h3>\n\n\n\n<p>One of the interesting things about this operator is that it now introduces a completely new way to perform assignment. In other words, it directly violates the \u201cthere should be only way way to do it\u201d rule.<\/p>\n\n\n\n<p>That said, I\u2019m a little bit on the fence with this one because the new operator is more explicit. In other words, it differentiates the intent behind <code>:=<\/code> and <code>=<\/code>. In addition, it reduces potential bugs related to confusing <code>=<\/code> and <code>==<\/code> in conditions.<\/p>\n\n\n\n<p>Likewise, as far as I can tell, you can\u2019t just use the walrus operator in all the same places you would use assignment. In fact, they\u2019re completely different operators. Unfortunately, nothing is really stopping you from doing something like this:<\/p>\n\n\n\n<div>\n<div id=\"highlighter_44661\" class=\"syntaxhighlighter  php\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"php plain\">(x := 5)<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n\n\n\n<p>I don\u2019t know why you would ever do this, but it\u2019s now very legal code. Luckily, <a href=\"https:\/\/www.python.org\/dev\/peps\/pep-0572\/#exceptional-cases\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">PEP 572 prohibits it<\/a>. Of course, that doesn\u2019t stop code like this from appearing in the wild. In fact, the documentation lists a handful of ways the new syntax can be abused. That\u2019s not a good sign!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">If the Implementation Is Hard to Explain, It\u2019s a Bad Idea<\/h3>\n\n\n\n<p>At this point, I sort of drew the line with this new feature. As I dug around to read others\u2019 opinions on the subject, I found the following gold nugget:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>This feels very Perl-y in the example given, in that it requires that you know what yet another operator means to read code that uses it. Since Python is supposed to be \u201cexecutable pseudocode\u201d (roughly), this kind of new operator might increase the amount of learning that a beginner has to do to read others\u2019 code. I hope that this decision does not pave the way for more like it, because it would make Python code much less readable to someone who hasn\u2019t studied the new operators yet.<i><cite><a rel=\"noreferrer noopener\" aria-label=\"snazz (opens in a new tab)\" href=\"https:\/\/news.ycombinator.com\/user?id=snazz\" target=\"_blank\">snazz<\/a>, 2019<\/cite><\/i><\/p><\/blockquote>\n\n\n\n<p>That\u2019s when I realized why I love Python so much. It\u2019s just so damn easy to read. At this point, I really feel like the addition of this operator was a mistake.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Counterpoint<\/h2>\n\n\n\n<p>As with anything, I hate to form an opinion without really digging into the topic, so I decided to hear from the folks who were excited about this feature. To my surprise, I found a lot of cool examples.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Loop Variable Updates Are Easy<\/h3>\n\n\n\n<p>By far, the strongest case for the new walrus operator is in while loops. Specifically, I liked the example by Dustin Ingram which leveraged the operator to remove duplicate lines of code. For example, we can convert this (<a href=\"https:\/\/speakerdeck.com\/di_codes\/pep-572-the-walrus-operator?slide=30\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"source (opens in a new tab)\">source<\/a>):<\/p>\n\n\n\n<div>\n<div id=\"highlighter_979043\" class=\"syntaxhighlighter  php\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<div class=\"line number3 index2 alt2\">3<\/div>\n<div class=\"line number4 index3 alt1\">4<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"php plain\">chunk = file.read(8192)<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"php keyword\">while<\/code> <code class=\"php plain\">chunk:<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"php spaces\">&nbsp;&nbsp;<\/code><code class=\"php plain\">process(chunk)<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"php spaces\">&nbsp;&nbsp;<\/code><code class=\"php plain\">chunk = file.read(8192)<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n\n\n\n<p>Into this:<\/p>\n\n\n\n<div>\n<div id=\"highlighter_11332\" class=\"syntaxhighlighter  php\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"php keyword\">while<\/code> <code class=\"php plain\">chunk := file.read(8192):<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"php spaces\">&nbsp;&nbsp;<\/code><code class=\"php plain\">process(chunk)<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n\n\n\n<p>By introducing the walrus operator, we remove a duplicate line of code. Now, every time the loop iterates, we automatically update <code>chunk<\/code> without having to initialize it or update it explicitly.<\/p>\n\n\n\n<p>Seeing this example is enough for me to see the value in the walrus operator. In fact, I\u2019m so impressed by this example that it made me wonder where else this could be used to improve existing code.<\/p>\n\n\n\n<p>That said, I did dig around, and some folks still felt like this was a bad example. After all, shouldn\u2019t file reading support an iterable? That way, we could use a for loop, and this wouldn\u2019t be an issue at all. In other words, <strong>isn\u2019t the walrus operator just covering up for bad library design?<\/strong> Perhaps.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">List Comprehensions Get a New Tool<\/h3>\n\n\n\n<p>As an avid list comprehension enthusiast, I\u2019ve found that the walrus operator can actually improve efficiency by allowing us to reuse calculations. For example, we might have a comprehension which looks like this:<\/p>\n\n\n\n<div>\n<div id=\"highlighter_889652\" class=\"syntaxhighlighter  php\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"php plain\">[determinant(m) <\/code><code class=\"php keyword\">for<\/code> <code class=\"php plain\">m in matrices <\/code><code class=\"php keyword\">if<\/code> <code class=\"php plain\">determinant(m) &gt; 0]<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n\n\n\n<p>In this example, we build up a list of determinants from a list of matrices. Of course, we only want to include matrices whose determinants are greater than zero.<\/p>\n\n\n\n<p>Unfortunately, the determinant calculation might be expensive. In addition, if we have a lot of matrices, calculating the determinant twice per matrix could be costly. Luckily, the walrus operator is here to help:<\/p>\n\n\n\n<div>\n<div id=\"highlighter_166472\" class=\"syntaxhighlighter  php\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"php plain\">[d <\/code><code class=\"php keyword\">for<\/code> <code class=\"php plain\">m in matrices <\/code><code class=\"php keyword\">if<\/code> <code class=\"php plain\">(d := determinant(m)) &gt; 0]<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n\n\n\n<p>Now, we only calculate the determinant once for each matrix. How slick is that?<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Miscellaneous<\/h3>\n\n\n\n<p>Beyond the two examples above, I\u2019ve seen a few other examples including pattern matching, but I don\u2019t really have an appreciation for it. Honestly, the other examples just seem kind of niche.<\/p>\n\n\n\n<p>For instance, <a href=\"https:\/\/www.python.org\/dev\/peps\/pep-0572\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"PEP 572 (opens in a new tab)\">PEP 572<\/a> states that the walrus operator helps with saving expensive computations. Of course, the example they provide is with constructing a list:<\/p>\n\n\n\n<div>\n<div id=\"highlighter_222800\" class=\"syntaxhighlighter  php\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"php plain\">[y := f(x), y**2, y**3]<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n\n\n\n<p>Here, we have a list that looks like this:<\/p>\n\n\n\n<div>\n<div id=\"highlighter_919301\" class=\"syntaxhighlighter  php\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"php plain\">[y, y**2, y**3]<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n\n\n\n<p>In other words, what\u2019s stopping us from declaring y on a separate line?<\/p>\n\n\n\n<div>\n<div id=\"highlighter_651832\" class=\"syntaxhighlighter  php\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"php plain\">y = f(x)<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"php plain\">[y, y**2, y**3]<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n\n\n\n<p>In the list comprehension example above, I get it, but here I don\u2019t. Perhaps there\u2019s a more detailed example which explains why we\u2019d need to embed an assignment statement in list creation. If you have one, feel free to share it in the comments.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Assessment<\/h2>\n\n\n\n<p>Now that I\u2019ve had a chance to look at the new walrus operator more or less objectively, I have to say that I think my first impressions still stand, but I\u2019m willing to be persuaded otherwise.<\/p>\n\n\n\n<p>After seeing a few solid examples, I was still really skeptical, so I decided to take a look at the rationale behind the operator in <a href=\"https:\/\/www.python.org\/dev\/peps\/pep-0572\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"PEP 572 (opens in a new tab)\">PEP 572<\/a>. If you get a chance, take a look at that document because it\u2019s enormous. Clearly, this decision was well thought out. My only fear is that the authors were persuaded to include the feature by shear <a href=\"https:\/\/www.behavioraleconomics.com\/resources\/mini-encyclopedia-of-be\/sunk-cost-fallacy\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"sunk cost fallacy (opens in a new tab)\">sunk cost fallacy<\/a>, but who knows.<\/p>\n\n\n\n<p>If you read through PEP 572, you\u2019ll see 79 code blocks across the entire page. To me, that\u2019s just a ridiculous amount of examples. To make matters worse, a large portion of the examples show edge cases where the operator <strong>won\u2019t work<\/strong> or <strong>wouldn\u2019t be ideal<\/strong> rather than where it would provide an edge. For instance, take a look at some of these examples:<\/p>\n\n\n\n<div>\n<div id=\"highlighter_970528\" class=\"syntaxhighlighter  php\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"php plain\">x = y = z = 0&nbsp; # Equivalent: (z := (y := (x := 0)))<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n\n\n\n<div>\n<div id=\"highlighter_81041\" class=\"syntaxhighlighter  php\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"php plain\">x = 1, 2&nbsp; # Sets x to (1, 2)<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"php plain\">(x := 1, 2)&nbsp; # Sets x to 1<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n\n\n\n<div>\n<div id=\"highlighter_476702\" class=\"syntaxhighlighter  php\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"php plain\">total += tax&nbsp; # Equivalent: (total := total + tax)<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n\n\n\n<p>That said, the authors did go as far as to <a href=\"https:\/\/www.python.org\/dev\/peps\/pep-0572\/#examples\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">provide some examples from their reworked standard library<\/a>. Of course, these examples are much larger, so I won\u2019t share them here. However, you\u2019re welcome to take a peek.<\/p>\n\n\n\n<p>Personally, I think the examples linked above illustrate the advantage of the walrus operator much better than some of the cases I shared in the counterpoint section. Specifically, any time the walrus operator removes duplicate or nested code, I\u2019m happy with it. Otherwise, it seems to have very few obvious use cases.<\/p>\n\n\n\n<p>My worry is that adding a new operator adds unnecessary complexity to the language, and I\u2019m not convinced that the pros outweigh the cons. At any rate, I trust the decision of the team that put it together, and I\u2019m excited to see how the community uses it!<\/p>\n\n\n\n<div class=\"attribution\">\n<table>\n<tbody>\n<tr>\n<td>\n<p>Published on Web Code Geeks with permission by Jeremy Grifski, partner at our <a href=\"\/\/www.webcodegeeks.com\/join-us\/wcg\/\" target=\"_blank\" rel=\"noopener noreferrer\">WCG program<\/a>. See the original article here: <a href=\"https:\/\/therenegadecoder.com\/code\/the-controversy-behind-the-walrus-operator-in-python\/\" target=\"_blank\" rel=\"noopener noreferrer\">The Controversy Behind The Walrus Operator in Python<\/a><\/p>\n<p>Opinions expressed by Web Code Geeks contributors are their own.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>If you haven\u2019t heard, Python 3.8 features a rather controversial new operator called the walrus operator. In this article, I\u2019ll share some of my first impressions as well as the views from all sides. Feel free to share some of your thoughts as well in the comments. Understanding the Walrus Operator Recently, I was browsing &hellip;<\/p>\n","protected":false},"author":14722,"featured_media":1651,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53],"tags":[],"class_list":["post-24877","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>The Controversy Behind The Walrus Operator in Python - Web Code Geeks - 2026<\/title>\n<meta name=\"description\" content=\"Interested to learn about Walrus Operator? Check our article sharing some of my first impressions as well as the views from all sides Walrus Operator\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/therenegadecoder.com\/code\/the-controversy-behind-the-walrus-operator-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"The Controversy Behind The Walrus Operator in Python - Web Code Geeks - 2026\" \/>\n<meta property=\"og:description\" content=\"Interested to learn about Walrus Operator? Check our article sharing some of my first impressions as well as the views from all sides Walrus Operator\" \/>\n<meta property=\"og:url\" content=\"https:\/\/therenegadecoder.com\/code\/the-controversy-behind-the-walrus-operator-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webcodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2019-11-25T10:50:27+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-06-23T12:01:09+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Jeremy Grifski\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@RenegadeCoder94\" \/>\n<meta name=\"twitter:site\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jeremy Grifski\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/therenegadecoder.com\/code\/the-controversy-behind-the-walrus-operator-in-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/the-controversy-behind-the-walrus-operator-in-python\/\"},\"author\":{\"name\":\"Jeremy Grifski\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/7eaa8e89cf23a5de82a048beb4c59aa1\"},\"headline\":\"The Controversy Behind The Walrus Operator in Python\",\"datePublished\":\"2019-11-25T10:50:27+00:00\",\"dateModified\":\"2020-06-23T12:01:09+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/the-controversy-behind-the-walrus-operator-in-python\/\"},\"wordCount\":1744,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/therenegadecoder.com\/code\/the-controversy-behind-the-walrus-operator-in-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/therenegadecoder.com\/code\/the-controversy-behind-the-walrus-operator-in-python\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/the-controversy-behind-the-walrus-operator-in-python\/\",\"url\":\"https:\/\/therenegadecoder.com\/code\/the-controversy-behind-the-walrus-operator-in-python\/\",\"name\":\"The Controversy Behind The Walrus Operator in Python - Web Code Geeks - 2026\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/therenegadecoder.com\/code\/the-controversy-behind-the-walrus-operator-in-python\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/therenegadecoder.com\/code\/the-controversy-behind-the-walrus-operator-in-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\",\"datePublished\":\"2019-11-25T10:50:27+00:00\",\"dateModified\":\"2020-06-23T12:01:09+00:00\",\"description\":\"Interested to learn about Walrus Operator? Check our article sharing some of my first impressions as well as the views from all sides Walrus Operator\",\"breadcrumb\":{\"@id\":\"https:\/\/therenegadecoder.com\/code\/the-controversy-behind-the-walrus-operator-in-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/therenegadecoder.com\/code\/the-controversy-behind-the-walrus-operator-in-python\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/therenegadecoder.com\/code\/the-controversy-behind-the-walrus-operator-in-python\/#primaryimage\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/therenegadecoder.com\/code\/the-controversy-behind-the-walrus-operator-in-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.webcodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\/\/www.webcodegeeks.com\/category\/python\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"The Controversy Behind The Walrus Operator in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"name\":\"Web Code Geeks\",\"description\":\"Web Developers Resource Center\",\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.webcodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webcodegeeks\",\"https:\/\/x.com\/webcodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/7eaa8e89cf23a5de82a048beb4c59aa1\",\"name\":\"Jeremy Grifski\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/9d50311a27dff0a54d775b6106b8f804d5d11d0b5182ed537f325046a020c6f5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/9d50311a27dff0a54d775b6106b8f804d5d11d0b5182ed537f325046a020c6f5?s=96&d=mm&r=g\",\"caption\":\"Jeremy Grifski\"},\"description\":\"Jeremy is the founder of The Renegade Coder, a software curriculum website launched in 2017. In addition, he is a PhD student with an interest in education and data visualization.\",\"sameAs\":[\"https:\/\/therenegadecoder.com\/\",\"https:\/\/www.linkedin.com\/in\/jeremy-grifski-22240552\/\",\"https:\/\/x.com\/RenegadeCoder94\"],\"url\":\"https:\/\/www.webcodegeeks.com\/author\/jeremy-grifski\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"The Controversy Behind The Walrus Operator in Python - Web Code Geeks - 2026","description":"Interested to learn about Walrus Operator? Check our article sharing some of my first impressions as well as the views from all sides Walrus Operator","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/therenegadecoder.com\/code\/the-controversy-behind-the-walrus-operator-in-python\/","og_locale":"en_US","og_type":"article","og_title":"The Controversy Behind The Walrus Operator in Python - Web Code Geeks - 2026","og_description":"Interested to learn about Walrus Operator? Check our article sharing some of my first impressions as well as the views from all sides Walrus Operator","og_url":"https:\/\/therenegadecoder.com\/code\/the-controversy-behind-the-walrus-operator-in-python\/","og_site_name":"Web Code Geeks","article_publisher":"https:\/\/www.facebook.com\/webcodegeeks","article_published_time":"2019-11-25T10:50:27+00:00","article_modified_time":"2020-06-23T12:01:09+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","type":"image\/jpeg"}],"author":"Jeremy Grifski","twitter_card":"summary_large_image","twitter_creator":"@RenegadeCoder94","twitter_site":"@webcodegeeks","twitter_misc":{"Written by":"Jeremy Grifski","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/therenegadecoder.com\/code\/the-controversy-behind-the-walrus-operator-in-python\/#article","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/python\/the-controversy-behind-the-walrus-operator-in-python\/"},"author":{"name":"Jeremy Grifski","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/7eaa8e89cf23a5de82a048beb4c59aa1"},"headline":"The Controversy Behind The Walrus Operator in Python","datePublished":"2019-11-25T10:50:27+00:00","dateModified":"2020-06-23T12:01:09+00:00","mainEntityOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/python\/the-controversy-behind-the-walrus-operator-in-python\/"},"wordCount":1744,"commentCount":0,"publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"image":{"@id":"https:\/\/therenegadecoder.com\/code\/the-controversy-behind-the-walrus-operator-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/therenegadecoder.com\/code\/the-controversy-behind-the-walrus-operator-in-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.webcodegeeks.com\/python\/the-controversy-behind-the-walrus-operator-in-python\/","url":"https:\/\/therenegadecoder.com\/code\/the-controversy-behind-the-walrus-operator-in-python\/","name":"The Controversy Behind The Walrus Operator in Python - Web Code Geeks - 2026","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/therenegadecoder.com\/code\/the-controversy-behind-the-walrus-operator-in-python\/#primaryimage"},"image":{"@id":"https:\/\/therenegadecoder.com\/code\/the-controversy-behind-the-walrus-operator-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","datePublished":"2019-11-25T10:50:27+00:00","dateModified":"2020-06-23T12:01:09+00:00","description":"Interested to learn about Walrus Operator? Check our article sharing some of my first impressions as well as the views from all sides Walrus Operator","breadcrumb":{"@id":"https:\/\/therenegadecoder.com\/code\/the-controversy-behind-the-walrus-operator-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/therenegadecoder.com\/code\/the-controversy-behind-the-walrus-operator-in-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/therenegadecoder.com\/code\/the-controversy-behind-the-walrus-operator-in-python\/#primaryimage","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/therenegadecoder.com\/code\/the-controversy-behind-the-walrus-operator-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.webcodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/www.webcodegeeks.com\/category\/python\/"},{"@type":"ListItem","position":3,"name":"The Controversy Behind The Walrus Operator in Python"}]},{"@type":"WebSite","@id":"https:\/\/www.webcodegeeks.com\/#website","url":"https:\/\/www.webcodegeeks.com\/","name":"Web Code Geeks","description":"Web Developers Resource Center","publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.webcodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.webcodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.webcodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webcodegeeks","https:\/\/x.com\/webcodegeeks"]},{"@type":"Person","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/7eaa8e89cf23a5de82a048beb4c59aa1","name":"Jeremy Grifski","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/9d50311a27dff0a54d775b6106b8f804d5d11d0b5182ed537f325046a020c6f5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/9d50311a27dff0a54d775b6106b8f804d5d11d0b5182ed537f325046a020c6f5?s=96&d=mm&r=g","caption":"Jeremy Grifski"},"description":"Jeremy is the founder of The Renegade Coder, a software curriculum website launched in 2017. In addition, he is a PhD student with an interest in education and data visualization.","sameAs":["https:\/\/therenegadecoder.com\/","https:\/\/www.linkedin.com\/in\/jeremy-grifski-22240552\/","https:\/\/x.com\/RenegadeCoder94"],"url":"https:\/\/www.webcodegeeks.com\/author\/jeremy-grifski\/"}]}},"_links":{"self":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/24877","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/users\/14722"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/comments?post=24877"}],"version-history":[{"count":0,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/24877\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media\/1651"}],"wp:attachment":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media?parent=24877"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/categories?post=24877"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/tags?post=24877"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}