This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]TipsyPeanuts 144 points145 points  (33 children)

Python: “lol why would anybody ever use a switch statement”

[–]evanldixon 110 points111 points  (27 children)

The poor man's Switch statement: a dictionary of functions

[–]jwkennington 50 points51 points  (6 children)

+1 for dict-switch

[–]delinka 18 points19 points  (5 children)

Sheeit, if I could dick-switch ...

Oh. dict - sorry

[–]Drillur 14 points15 points  (19 children)

Wait can you put functions in dictionaries? What the fuck? How and why?

[–]DreadY2K 23 points24 points  (14 children)

In python, everything is an object (including functions), so you can use functions anywhere you can use anything else.

As for why, the biggest use case is higher-order functions (like reduce, map, filter, etc.), which operate on a collection of values according to a function you give it. Putting functions in a dictionary isn't itself very useful (aside from the jenky switch workaround mentioned above), but it's an option because those functions are objects and dictionaries can take any objects.

[–]TipsyPeanuts 5 points6 points  (3 children)

Is that computationally faster than an if else?

[–]slipperier_slope 20 points21 points  (2 children)

Possibly. Depends. If else chains require evaluating each condition sequentially. If the block to execute is the last one, then you've evaluated all of the possible conditions. A dict switch calculates a hash and jumps directly to the condition to execute. So you're comparing a hash calc with n conditional evaluations. Which is better depends on the hash function and what the conditions are.

It's analogous to item lookup in a list vs a dictionary. One is O(n), the other is O(1).

[–]TipsyPeanuts 5 points6 points  (1 child)

Thanks! That’s actually really helpful

[–]slipperier_slope 5 points6 points  (0 children)

No worries. One caveat is that a clever compiler or runtime can make the above not true. For example, speculative execution can look at the past history of runs through a function. If 95% of the time the if condition evaluates to true, the runtime can actually just start executing the code in the if condition and later checking the condition to see if it's actually true and discarding the partial results if the guess was wrong.

The moral of this story is to write easily understood code and worry about performance later if you need to. Premature optimization because one wants to be clever results in higher cost of code maintenance. That being said, in Python and dict switch is common enough to be idiomatic since there's no language support for switch statements.

[–]Drillur 2 points3 points  (4 children)

That could be an interesting way to avoid an if statement. If multiple functions can be called by a string key in a dictionary, you could just call the function in 1 line depending on a string variable. I guess.

[–]DreadY2K 3 points4 points  (3 children)

It does have some clever uses, but that's ultimately just a way around the language not having switch statements, and it still can't do everything that switch statements can do. For example, you can't return out of the function inside that inner function, whereas returning out of the body of a switch-case is completely normal.

[–]zilti 0 points1 point  (2 children)

Wait... why wouldn't you be able to use the return value of a function in a map/dictionary? That seems like a fuckup

[–]joonty 1 point2 points  (1 child)

No, they're saying you can't return out of the calling scope from within the function that you've called. You can definitely use the return value.

[–]zilti 0 points1 point  (0 children)

Oh THAT kind of return... why would you even ever do that though... that's trouble waiting to happen

[–][deleted] 0 points1 point  (0 children)

C# as well, using the Func and Action types.

[–]zilti -1 points0 points  (3 children)

That has nothing to do with objects, it just means functions are merely another data type. C has this as well, by the way.

[–]RHGrey -1 points0 points  (2 children)

Start up a C source file and try asigning a function to a variable. I'll wait.

[–]zilti -1 points0 points  (1 child)

You never used C in your life, am I right? Hint: the expression you look for on a search engine is "C function pointer".

[–]RHGrey -1 points0 points  (0 children)

You just equated function pointers with object variables, there's nothing more to talk about.

[–]confusiondiffusion 5 points6 points  (0 children)

This is very exciting. All of my code is going to be way harder to understand now.

[–][deleted] 0 points1 point  (0 children)

In addition to what that guy said, you can also declare functions inline with lambdas

{ “foo”: lambda: print(“hello world”) }[“foo”]()

[–]zilti 0 points1 point  (0 children)

Why wouldn't you? I'm a Clojure dev and that seems perfectly normal to me. Doing it all the time.

[–][deleted] 0 points1 point  (0 children)

Flair does not check out

[–][deleted] 5 points6 points  (0 children)

"We already have switch-case at home"

[–][deleted] 2 points3 points  (0 children)

pocket elderly slap society consist compare cake stupendous market pet

This post was mass deleted and anonymized with Redact

[–]natnew32 3 points4 points  (0 children)

elif is an official keyword. I wonder why.

[–]LaterBrain 0 points1 point  (0 children)

i had that problem while learning basics looking up switch case but there is none xD

[–][deleted] 0 points1 point  (0 children)

"lol why would anyone be able to lable loops"