You, a coward: Python doesn't have a switch statement

Me, a modern-day Galileo:

@christianp that's so... ugly.

@christianp Nooo...

noooooooo

@christianp I....

...

...

I mean, seeing some commonly used pythonisms, this is not even the worst to be honest

@petra @christianp HI THERE IVE MISSED SEEING UR TWEETS AAaaa <3

@christianp you monster

@Christian Lawson-Perfect Uhhh... I hope no one actually does that :D

This is the Pythonic way:

In [1]: x = 1 ...: { ...: 1: "One's lonely", ...: 4: "It's four!", ...: }.get(x*x, "Some other number") ...: Out[1]: "One's lonely"

@jaywink @christianp I wouldn't call that "Pythonic" (Though I've done it), I would call that "A desperate cry for the Python devs to actually cater to real world requirements because look at us, rutting like animals in deeply nested spaghetti code, birthing convoluted tracebacks as if trying to snare some semblance of order from a fragmented world"

@christianp because you can doens't mean you should

@zaun I say that to myself every time I get out of bed

@christianp hmmm... can we wrap that "if s not in memo" into a defaultdict to obscure it even more?

@christianp Having done similar stuff in Java, I have a *very* bad gut feeling about this. Can't really explain, but it feels all like this is not going to end well. 🙂

"Mom, can we have switch statements?"

"We have switch statements at home."

Switch statements at home:

@christianp this is so evil :)

@christianp This actually is pretty neat because if you leave out the default case, any input not listed as a case will raise an exception. Automatic bounds checking!

@invis yeah, after I tooted that I realised I should've made all the cases subclasses of a generic CaseException, then you can catch the default case and real exceptions separately.

... if you were doing this in real life, which you shouldn't

@christianp
very nice! could be done with metaclasses to have pretty syntax like Case.First and Case.Second

@kilomeow where would you say what each case corresponds to?

@christianp I have never seen such exceptional abuse 👮 you have the right to remain silent

@christianp The novice writes JavaScript, the apprentice writes Python, and the master can write FORTRAN in anything.

@christianp today in code review is hard, I'm looking at this and thinking there really is no need for json encoding, it's even being done twice.. and maybe that's not the problem here xD

The social network of the future: No ads, no corporate surveillance, ethical design, and decentralization! Own your data with Mastodon!