Follow

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

Me, a modern-day Galileo:

@christianp I....

...

...

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

@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"
See also: Anything functional-programmy in Python

@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. 🙂

@christianp

"Mom, can we have switch statements?"

"We have switch statements at home."

Switch statements at home:

@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`

@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
Sign in to participate in the conversation
Mathstodon

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