DEV Community

Cover image for The Controversy Behind The Walrus Operator in Python

The Controversy Behind The Walrus Operator in Python

Jeremy Grifski on November 08, 2019

If you haven’t heard, Python 3.8 features a rather controversial new operator called the walrus operator. In this article, I’ll share some of my fi...
Collapse
 
mickleby profile image
Mickleby

Great article! Thank you

As this is the behavior of C, I presume this behavior was consciously omitted in Python. Is there documentation of the original decision?

I look on this with the expectation of having a pre-processor. Thus I ask myself, why not just declare a macro for those who will use it? This is in essence how Objective-C works.

"Those who will use it." The "in favor" examples read to me (with my C-dev eyes) as already rather exotic. If a wide-spread use of Python already strays from Zen perhaps this is the interesting thing, the ideal vs living language. That walrus took some heat is a footnote, prompting interest in why now? why not then?

Another note I don't seem to find: van Rossum resigned as BDFL in 2018. I'd like to read thoughts about how this resignation impacted walrus. Some people seem to suggest walrus caused resignation, but I'm not seeing data to support this view. To what extent is walrus simply a stretching out, given Dad no longer does our driving?

Perhaps the story is idealistic youth gives way to practicality in deployment, peeling out as sheer pleasure. Who wants Peter Pan, still uses Python?

Collapse
 
steelwolf180 profile image
Max Ong Zong Bao

It feels like a pain to read the code since essentially your putting 2 lines of code together.

Sure in terms of optimisation and cleverness is good but my only worry is that is its maintainability of this piece of code.

Even for string literals despite it has been out for 2 years. It's only recently that I started using it in my python code as it was not within my purview previously on it's usefullness.

Collapse
 
renegadecoder94 profile image
Jeremy Grifski • Edited

Yeah, I’m generally against increasing the complexity of languages. That’s why I like python so much to begin with. I feel like this doesn’t add enough for it to be worth it.

Collapse
 
mickleby profile image
Mickleby

I take note of the framing "complexity of language." We have a living industry, existing and future code, folks engaging source, tasks to perform, data to wrangle. The optimal interface (language, Python) may not be apparent, not obvious. Certain abstractions (language design decisions) optimize different engagements, even as the user base (coders) is constantly evolving, both in response and orthogonal to the language, the tasks, the install base.

To me the ideal language is one that prompts me to ask the insightful questions and to gloss all else -- given the domain on which I apply this language. [the right complexity -- and only that much -- for the job at hand]

Collapse
 
aadibajpai profile image
Aadi Bajpai

Honestly, I'm really happy it's coming. I was a bit apprehensive too initially but ever since then I have started noticing places where the walrus operator would really simplify my code. I don't like writing the double lines personally, I really feel this optimization is nice.

Collapse
 
renegadecoder94 profile image
Jeremy Grifski

Do you have any cool examples? I’m actually pretty excited to see how the community uses this operator.

Collapse
 
aadibajpai profile image
Aadi Bajpai

Have a cool example now. Calculating diff b/w subsequent values.

>>> data = [10, 14, 34, 49, 70, 77]
>>> prev = 0; 
>>> [-prev + (prev := x) for x in data]
[10, 4, 20, 15, 21, 7]
Collapse
 
aadibajpai profile image
Aadi Bajpai

I have yet to port my bigger projects to it but if I write something cool I'll let ya know.

Collapse
 
victotronics profile image
Victor Eijkhout

I follow your argument but I don't agree. Why should statements be different from expressions? If you follow the principle that "every expression has a value" and "a statement is an expression" then life becomes a lot more elegant and orthogonal. The walrus operator (and the C++ initializer statement) are just a small steps towards my first language, Algol68 where you could write

if BEGIN char c; read(c); do something with c; is_upcase(c) END 
Enter fullscreen mode Exit fullscreen mode

See? A block is a statement is an expression, and an expression has a value.

Collapse
 
keith profile image
Keith Luu

In the Miscellaneous part, I think the walrus operator may come in handy when we are working with map():

x = [1, 2, 3]
map(lambda y: [z := f(y), z**2, z**3], x)
Collapse
 
renegadecoder94 profile image
Jeremy Grifski

This is a good one!

Collapse
 
__orderandchaos profile image
Order & Chaos Creative

It's for code golf right?

Collapse
 
renegadecoder94 profile image
Jeremy Grifski

But actually haha

Collapse
 
renegadecoder94 profile image
Jeremy Grifski

Yeah, I like any use case where this operator removes duplicate code or computations, but I haven’t seen enough examples to be fully on board yet.