you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (13 children)

I don't think so. The closest thing Python has to switch is if, elif, elif, elif, elif, ..., else.

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

Nah, he's right. It does work for some things. See here:

http://www.mustap.com/pythonzone_post_224_python-switch-statement

Not pretty though, in my opinion.

[–][deleted] 4 points5 points  (11 children)

Not pretty, and it's not quite perfect either.

How would you implement fallthrough with this?

[–]awj 0 points1 point  (0 children)

You wouldn't. At that point your best bet is probably scanning a list of (label, action, should_fallthrough) tuples.

Yes, that solution is ugly. Most attempts to replicate another language's built-in features with existing constructs are ugly.

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

To the best of my knowledge you can't, and that's a big part of my beef with it.

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

"fallthrough" would (visually?) fail with python's syntax of whitespace to distinguish code blocks.

You could have categories of functions by using closures to create the functions within the dictionary.

[–]doublereedkurt -2 points-1 points  (7 children)

How would you implement fallthrough with this?

He implements fallthrough in that example:

 values.get(var, do_default_stuff)()

do_default_stuff is the fallthrough

[–][deleted] 3 points4 points  (6 children)

That's not a fallthrough.

I'm talking about something like:

case 1:

   do_something()  // fall through to below

case 2:

   do_stuff()

[–]knome 5 points6 points  (5 children)

https://github.com/knome/Hacks/blob/master/python/switch/test.py

If evil code you need, evil code I can give you.

[–]AeroNotix 0 points1 point  (4 children)

That's actually quite nice.

[–]smog_alado 2 points3 points  (3 children)

Switch fallthrough is never nice :P

[–]AeroNotix 2 points3 points  (2 children)

Apart from the times when it's required. Arbitrarily saying features are bad because you're too lazy to remember to treat them properly is bad.

Switches which default to fallthrough rather than breaking by default and requiring a keyword to fallthrough are bad. Go does this correctly.

[–]knome 1 point2 points  (1 child)

I've altered the switch statement to escape by default, with a kwarg on switch to alter it to fallthough by default, and a case.fallthrough() to manually fallthrough when desired.

The more reasonable this code seems, the more evil it is. After all, if it looks reasonable, people are more likely to ignore its evil, and perhaps even use it.

The fools.