@christianp I....
...
...
I mean, seeing some commonly used pythonisms, this is not even the worst to be honest
@christianp n-no!
@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"
See also: Anything functional-programmy in Python
@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. 🙂
@christianp this is so evil :)
@christianp wow
@christianp you're a bad man.
@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 aua
@christianp The novice writes JavaScript, the apprentice writes Python, and the master can write FORTRAN in anything.
@christianp ;_;
@christianp
They added one in 3.10
of course, this has been done before: http://code.activestate.com/recipes/410695-exception-based-switch-case/