Skip to content
Sign upLog in
← Back to Community

Does Python have an equivalent to Haxe switch cases?

Profile icon

I do a lot of programming in Haxe, which is honestly one of my favorite coding languages because of how easy it is to get used to. And one of the things I use the most in Haxe is these things called switch cases. Here's an example:

var number = 1; switch(number) { case 0: trace("Number is 0"); // haxe equivalent of print case 1: trace("Number is 1"); default: trace("Number was not 0 or 1"); }

It's basically a really compact way of checking if a variable matches several values.

I'm wondering if Python has an equivalent to the Switch case or if I have to stick to messy if statements.

Answered by Coder100 [earned 5 cycles]
View Answer
Profile icon
Profile icon

python does not.
you can in a way make a 'switch'

conds = [(lambda: True, lambda: print("switch!")), (lambda: False, lambda: print("lol"))] for (cond, done) in conds: if cond(): done() break
Profile icon

You can try using if statements