r/badcode • • Apr 30 '22

python Looks like Python List Comprehension-phobia is a thing. This is a real commit by a teammate.

Post image
1.2k Upvotes

125 comments sorted by

View all comments

422

u/imthebear11 Apr 30 '22

This is also what gets me when people are against the assignment expression operator(:=) because they claim, "well it can be confusing if you don't know what it is"... like yeah all code is confusing if you don't know what it is. It's like people don't want to grow and want to keep writing the same code that would run on Python 2.5

96

u/FordyO_o May 01 '22

Someone I used to work with (who had a pretty hot temper) tried to ban object destructuring in JS because they didn't understand it

26

u/hey--canyounot_ May 01 '22

Lmao bruh...any other good stories about them?

12

u/FordyO_o May 01 '22

Nothing that comes to mind, they were pretty nice to work with most of the time, just became enraged when they got stuck on anything 😃

6

u/raam86 May 01 '22

back in the day jetbrains syntax highlighting support for scala wasn’t perfect.

in scala object.prop == (object prop). you can replace dot with space and sometimes syntax highlighting would mark it in squiggly reds which is annoying so i would add dots where it would do that or where it would make sense.

I would use space when it made sense as a dsl like account deposit 300 usd

one week the principle engineer became more and more irritated and wouldn’t admit to it or share his reason. A weekend comes in, he is at the end of this rope and he tells he can’t understand why i put a dot here and not there. i explain the above and he refuses to accept this, I explain again that you can simply drop the dot sometimes and i do it for aesthetics. as far as i know he still doesn’t accept it because they threw away the scala code and replaced it with beam

3

u/lordheart May 07 '22

Omg, been doing Java at work and I miss de structuring so much

4

u/FordyO_o May 07 '22

At least you get strict typing to cheer you up a bit 👍

3

u/lordheart May 07 '22

I usually do typescript, and that adds enough strictness 😁

4

u/FordyO_o May 07 '22

Lucky you, I've only recently managed to migrate our project to ES6, pray for me

2

u/lordheart May 07 '22

I get to be the trail blazer at my new job for typescript projects as well

68

u/JestemStefan Apr 30 '22

Luckily we started using walrus operator as soon as we upgraded to Python 3.8. Coming next: match statements after 3.10 upgrade

1

u/wtfzambo May 29 '22

Care to enlighten me with a real use case scenario where it's useful to have it?

I use anything from python 3.7+ and except once, I never found myself using it.

2

u/JestemStefan May 29 '22 edited May 29 '22

Instead of

    if some_var:
        x = some_var
        # rest of code

You can do:

    if x := some_var:
        # rest of code

1

u/wtfzambo May 29 '22

Seems such a weak improvement :(

Is the second x scoped solely within the if statement?

So that if I call x outside of it I get NameError?

3

u/JestemStefan May 29 '22 edited May 29 '22

It's available in outer scode.

You can also do:

    if not (x := some_var):
        #some code
    
    print(x)  #works

It works like:

    x = some_var
    if x:
        #rest of code

Anyway. You save one line of code and you don't repeat yourself

1

u/wtfzambo May 29 '22

Then I really don't understand the purpose. Maybe I'm too stupid lol.

3

u/JestemStefan May 29 '22 edited May 29 '22

You can assign variable and use it in if statement at the same time.

You save line of code and you don't repeat yourself

Small QoL improvement.

→ More replies (2)

29

u/droomph May 01 '22

An electronic computer can be confusing when you don’t know what it is, so therefore I propose we go back to warehouses of women working as computers

4

u/Da-Blue-Guy May 01 '22

When I do code golf challenges or just want to see how concise I can make something, the walrus operator is perfect. It does have its uses.

4

u/sammcell May 01 '22

I have been using python for at least a year or two and have never seen that before. Thank you for enlightening me.

3

u/fridge_water_filter May 01 '22

I've been using python for decades and never use it.

It is a stylistic thing for the most part.

3

u/[deleted] May 01 '22

I had someone who didn't like the safe navigation operator in groovy for that same reason. I just had to roll my eyes and change the code so he'd approve the PR

2

u/[deleted] May 01 '22

Yeah that's odd. I can understand if people worry about line clutter, but understanding there is straightforward.