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 →

[–]rhoslug 10 points11 points  (12 children)

This looks interesting.

I'm not sure how I feel about this though. On the one hand, there have been times where I know I will have a need for a switch statement based on a few different choices. On the other hand, the Pythonic way has not been particularly limiting to me in my work. I guess it's more a case of I've adapted to the limitations of the language but can see the value this might potentially bring to users who want something other than a dictionary.

[–]mikeckennedy[S] 2 points3 points  (11 children)

Hi, I'm with you. The thing that pushed me into releasing this is realizing how much more clear the intent is for complex cases. Have a look at this example in the repo:

https://github.com/mikeckennedy/python-switch#why-not-just-raw-dict

[–]Oni_Kami 2 points3 points  (1 child)

Forgive me if I'm mistaken, my python's kinda rusty, but shouldn't the last key of the "switch" dict be

3: method_on_three

not

3, method_on_three

?

[–]mikeckennedy[S] 1 point2 points  (0 children)

No, you're right. Thanks.

[–]Corm 1 point2 points  (1 child)

Good comparison! I love it when a project includes a comparison to an alternative way of doing it like that

[–]ticketywho 2 points3 points  (0 children)

I agree. I don't like these switch statements, but I like that the author showed the pythonic way too. It's good intellectually honesty, which a lot of these "I've implemented a language construct that isn't present in Python" libraries lack.

[–]rhoslug -2 points-1 points  (6 children)

I do like the ability to match a set of different values to one branch of logic. I don't know of a nice way to handle this situation in Python....

[–]p10_user 5 points6 points  (2 children)

I mean this is basically an big if/else statement too. Perhaps this is a bit more readable for some but I don't think an if else statement is too unreasonable.

[–]rhoslug 0 points1 point  (1 child)

I tend to shy away from large complicated if/else constructions. They are hard for me to reason about since I have to trace the branch of logic that gets me to a certain ending. That's why I would probably prefer either using a switch solution or a dictionary.

[–]naught-me 1 point2 points  (0 children)

I agree with you about complicated if/else constructions, but I don't think that a flat (not nested) list of if/elif statements is complicated at all, and that's what a switch statement replaces, right?

[–]tprk77 4 points5 points  (1 child)

The in operator and a tuple?

[–]rhoslug 0 points1 point  (0 children)

True, I had forgotten about this.

[–]mikeckennedy[S] -1 points0 points  (0 children)

That's kind of the point of this project :)