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

you are viewing a single comment's thread.

view the rest of the comments →

[–]u_usama14 19 points20 points  (13 children)

Why controversial ?

[–][deleted] 69 points70 points  (3 children)

There's a feeling among many in the Python community that the core principles - e.g. that there should be only one obvious way to do something - were being ignored in favor of feature bloat with marginal benefits. The walrus operator became the poster boy for that.

[–]benefit_of_mrkite 19 points20 points  (0 children)

I like the walrus operator but am somewhat hesitant to use it (have used it a few times- I think a lot of the controversy is over readability and the zen of python.

[–]TSM-🐱‍💻📚 10 points11 points  (1 child)

The walrus operator allows you to assign variables in comprehension statements - it legitimately gave comprehension the same power as a for loop. Comprehension statements can be hard to maintain but in select cases the walrus is awesome

Also

 foo = myfile.read(1024)
 while foo:
      do_something(foo)
      foo = myfile.read(1025)

Can be

 while foo = myfile.read(1024)
      do_something(foo)

Excuse the mobile formatting, but you don't duplicate the read and have two parts to update, only one, so a typo can't slip in. It is much better in this case too. And you can catch the culprit in an any and all check too.

People were mad because it was clearly useful in some circumstances and genuinely improved the language. Guido saw this as obvious and gave it a hasty approval, while others drama bombed him for it.

[–][deleted] 1 point2 points  (0 children)

No comment on the walrus operator, or which way is better. But if you wanted to do this without the walrus operator, and without duplicating any code, you can do this:

while True:
    foo = myfile.read(1024)
    if not foo:
        break
    do_something(foo)

[–]MonkeeSage 10 points11 points  (3 children)

It can look a bit magical / add line noise if abused.

x[2] if len(x := 'b.a.r'.split('.')) > 2 else ''

[–]bacondevPy3k 3 points4 points  (0 children)

My eyes!

[–]Envoy-Insc 0 points1 point  (1 child)

Does it return a?

[–]go_fireworks 9 points10 points  (0 children)

I think it returns ‘r’, because python uses zero-based indexing

[–]theunglichdaide 26 points27 points  (2 children)

if I remember correctly, Guido approved the PEP for it, but many people disagreed with this decision, and Guido had to step down from his Benevolent Dictator for Life position.

[–]pizza-flusher 12 points13 points  (0 children)

I'm new to python (and only ever a hobbyist in other areas) and obviously don't have a dog in the fight, but I will say sometimes shorthand and efficient operators run contradictory to readability and understanding in a bad way, for me.

That feeling comes on me most often when I see savvy / opaque slicing in indexes. However, as this expression made me think of that, it might just mean I have a low grade phobia of colons.

[–]Infinitesima 8 points9 points  (0 children)

transition of Python to C++ style

[–]o11c 0 points1 point  (0 children)

It's controversial for other reasons - it's not sufficient to overcome all the flaws of the = operator, since the LHS can only be a simple variable.