this post was submitted on 15 Sep 2023
390 points (93.5% liked)

Programmer Humor

19480 readers
203 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 1 year ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 6 points 1 year ago* (last edited 1 year ago) (1 children)
def count_fingers(hand: list): 
    count = len(hand)
    if count != 5:
        if count < 5:
            raise Exception("Check if fingers missing, or just smart ass")
        else:
            raise Exception("Oh... oh no.")
    return count
[–] [email protected] 5 points 1 year ago (1 children)

You can do

if (count := len(hand)) != 5:
    # do something with "count"

Btw, looks much nicer

[–] [email protected] 0 points 1 year ago (1 children)

I'll use ternary operators when you force them into my cold, dead hands

[–] [email protected] 7 points 1 year ago (1 children)

It's not the ternary operator, it is the walrus operator introduced in Python 3.8 if I'm correct

[–] [email protected] 5 points 1 year ago

You are correct. I came back to say that I'll use the walrus operator when it's pushed into my cold, dead hands, but... I might actually use it, now that I've refreshed myself on it.