hairinmybellybutt@lemmy.world to Programmer Humor@programming.devEnglish · 2 年前call the doctor, the CS doctorlemmy.worldimagemessage-square44fedilinkarrow-up1419arrow-down129
arrow-up1390arrow-down1imagecall the doctor, the CS doctorlemmy.worldhairinmybellybutt@lemmy.world to Programmer Humor@programming.devEnglish · 2 年前message-square44fedilink
minus-squareconditional_soup@lemm.eelinkfedilinkarrow-up7arrow-down1·edit-22 年前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
minus-squareHurglet@lemmy.basedcount.comlinkfedilinkarrow-up6arrow-down1·2 年前You can do if (count := len(hand)) != 5: # do something with "count" Btw, looks much nicer
minus-squareconditional_soup@lemm.eelinkfedilinkarrow-up1arrow-down1·2 年前I’ll use ternary operators when you force them into my cold, dead hands
minus-squareHurglet@lemmy.basedcount.comlinkfedilinkarrow-up7·2 年前It’s not the ternary operator, it is the walrus operator introduced in Python 3.8 if I’m correct
minus-squareconditional_soup@lemm.eelinkfedilinkarrow-up6arrow-down1·2 年前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.
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
You can do
if (count := len(hand)) != 5: # do something with "count"
Btw, looks much nicer
I’ll use ternary operators when you force them into my cold, dead hands
It’s not the ternary operator, it is the walrus operator introduced in Python 3.8 if I’m correct
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.