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...
For further actions, you may consider blocking this person and/or reporting abuse
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?
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.
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.
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]
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.
Do you have any cool examples? I’m actually pretty excited to see how the community uses this operator.
Have a cool example now. Calculating diff b/w subsequent values.
I have yet to port my bigger projects to it but if I write something cool I'll let ya know.
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
See? A block is a statement is an expression, and an expression has a value.
In the Miscellaneous part, I think the walrus operator may come in handy when we are working with map():
This is a good one!
It's for code golf right?
But actually haha
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.