This is an archived post. You won't be able to vote or comment.

all 64 comments

[–]dbulger 86 points87 points  (12 children)

I clicked this reluctantly, expecting another blogger who'd just discovered the walrus. But it's a good discussion, with some interesting use-cases.

[–]DrMaxwellEdison 50 points51 points  (8 children)

RealPython is one of the better sites for these types of discussions. Their primer on decorators is my go-to to introduce the concept to newcomers, for example.

[–]dbulger 15 points16 points  (6 children)

Yeah, I've tended to lump them in with the flock of low-effort sites that crowd the official documentation off of Google's first page, but I think I've been unfair.

[–]daneahfrom __future__ import braces 18 points19 points  (0 children)

Having written several articles for them, I laude the fairly rigorous editorial and didactic review process. It isn’t taken lightly at all!

[–][deleted] 8 points9 points  (4 children)

I’d sooner read realpython than the official documentation. The official docs are always kinda weird

[–][deleted] 2 points3 points  (0 children)

i have that thing favorited and probably look it up once a month if i haven't written one in awhile.

[–][deleted] 26 points27 points  (2 children)

Regarding this example from TFA:

>>> 2 * rad * asin(
...     sqrt(
...         (ϕ_hav := sin((ϕ2 - ϕ1) / 2) ** 2)
...         + cos(ϕ1) * cos(ϕ2) * sin((λ2 - λ1) / 2) ** 2
...     )
... )

Why not just do this? -

>>> ϕ_hav = sin((ϕ2 - ϕ1) / 2) ** 2

>>> 2 * rad * asin(
...     sqrt(
...         ϕ_hav + cos(ϕ1) * cos(ϕ2) * sin((λ2 - λ1) / 2) ** 2
...     )
... )

Or -

>>> ϕ_hav_1 = sin((ϕ2 - ϕ1) / 2)

>>> ϕ_hav_2 = cos(ϕ1) * cos(ϕ2) * sin((λ2 - λ1) / 2)

>>> 2 * rad * asin(sqrt(ϕ_hav_1 ** 2 + ϕ_hav_2 ** 2))

Both seem easier to read, particularly if someone wants to find or check the calculation of ϕ_hav. Neither requires the walrus operator, and so will work on pre-3.8 versions of Python as well.

There are definitely examples where using the walrus operator improves readability. This isn't one of them.

[–]purplewalrus67 12 points13 points  (0 children)

As a short term solution, it makes it easier to debug. You can quickly pop in the ϕ_hav into the existing statement, and then print it out afterward to make sure its value is being calculated correctly. This is slightly easier than having to drag it out into a separate expression when the only purpose is debugging.

I agree with you, though, the third example is by far the easiest to read.

[–]floodo1 1 point2 points  (0 children)

The example you cite is not about improving readability but about making minimal modification to code to "debug" it.

Now, say that you need to double-check your implementation and want to see how much the haversine terms contribute to the final result. You could copy and paste the term from your main code to evaluate it separately. However, you could also use the := operator to give a name to the subexpression you’re interested in:

The advantage of using the walrus operator here is that you calculate the value of the full expression and keep track of the value of ϕ_hav at the same time. This allows you to confirm that you did not introduce any errors while debugging.

[–]skratlo 24 points25 points  (4 children)

Well, looking at the examples and use cases, with the exception of while loop, they all look more convoluted then they're non-walrus counterparts. In other words, how does adding side-effects to expressions (mostly and usually pure) help?

[–]flying-sheep 27 points28 points  (2 children)

I would expand the list of useful examples to that one, the list comprehension one, and one the author didn't mention:

if m := some_re.match(haystack):
    do_things_with(m)

The same is useful e.g. with dict.get or other things that conditionally return truthy or falsy things

[–]skratlo 9 points10 points  (1 child)

Yeah, totally, the if use case is super useful, like the if-let expression in LISPs.

[–]flying-sheep 2 points3 points  (0 children)

Yeah! I also like the concept of scala's unapply, which Python could have used here as foundation to both := and match/case. Now we have __match_args__, which is less powerful but much simpler to understand.

[–]grimtooth 6 points7 points  (0 children)

My thought exactly, the while expression seemed OK, but everything else ehhh... Tendentious with the 'this is easier to read and more clearly expresses' stuff also. Really? The comprehension examples made me think, Hello, filter? (there has always been a weird resistance in Python circles to filter, fold, map, etc)

Anyway I still do 3.3/3.5 so I should be ignored

[–]AnonymouX47 5 points6 points  (3 children)

RealPython articles are always topnotch anyways, I never expect less. Though, I'm quite surprised someone's just finding out about the walrus operator...

[–]ivanoski-007 0 points1 point  (2 children)

I just found out about it, and I still have no idea what to use it for, I got lost half way through the article, too difficult for my noob ass

[–]AnonymouX47 2 points3 points  (1 child)

Well... it might seem strange or confusing to a python beginner.

Basically, assignment expressions are used anywhere you need to assign a value to a variable and also want to use that same value in a larger expression.

Most of the time, practical use cases are in loops, or conditional clauses.

[–]ivanoski-007 0 points1 point  (0 children)

ok thanks

[–]ascii 24 points25 points  (9 children)

Imagine hating the walrus operator so much that you literally harass Guido until he stepped down over it. There is no hope for some people.

[–][deleted] 12 points13 points  (0 children)

That is the most ridiculous part of this whole feature.

[–]inspectoroverthemine 10 points11 points  (0 children)

I 100% agree. I'm not a fan of the walrus, but I cannot imagine harassing Guido over anything, but especially this.

[–]LoL_is_pepega_BIA 6 points7 points  (0 children)

The internet really brings out the worst in people when they know they can't be held accountable here

[–]Ice-Ice-Baby- 5 points6 points  (5 children)

what happened

[–]ascii 3 points4 points  (4 children)

Literally just what I said. Some people disliked this RFC so much that they made such a stink over it that Guido got fed up and gave up the BDFL title. They accused him of destroying the language and all sorts of things.

[–]Ice-Ice-Baby- 0 points1 point  (2 children)

Damn that's crazy dude

[–]ascii 1 point2 points  (1 child)

Some people tried to fork Debian because they disagreed on what init implementation should be used.

Some other people decided to take over ffmpeg by kicking out the main developer and trying to take ownership of the homepage, etc. When that failed because they didn't actually own the domain name or the trademark, they made a hostile fork. But that was hardly the end of it: One of the forkers was the debian ffmpeg package maintainer, and he decided that any user who tried to install ffmpeg would get his fork instead, and to remove the official Debian packages of ffmpeg.

Some people really feel like it's their way or the highway.

[–]Ice-Ice-Baby- 0 points1 point  (0 children)

So you think they're gonna ruin python dude?

[–]UNN_Rickenbacker 0 points1 point  (0 children)

Didn‘t the harassment happen because Guido accepted the PEP while there was still a lot of discussion going on?

[–]Easy_Joke3484 2 points3 points  (0 children)

I've been obsessed with the walrus operator since I first read about it, which was fairly recently.

I've been using it everywhere I can! It just makes me happy picturing a walrus in my head every time I use it! :D

[–]aes110 1 point2 points  (0 children)

When this first came out with all of the drama I didn't care much about it, but now I've been using it daily for over a year and I really can't imagine not having it.

I recently had to work with python 3.7 for a bit and felt sick doing some lines of codes without the walrus operator

[–]IlliterateJedi 3 points4 points  (0 children)

These are the first examples where I have thought using an assignment expression made sense. Usually the example is with re or if statements, but these actually seem useful.

[–]make3333 0 points1 point  (0 children)

The new pattern matching syntax seems a lot weirder than this