Greetings
Question for you. Does python suffer from a lack of switch / case statement in your opinions. I say yes, if / elif is OK but seems very inelegant to me.
Your opinions please.
Mark
Greetings
Question for you. Does python suffer from a lack of switch / case statement in your opinions. I say yes, if / elif is OK but seems very inelegant to me.
Your opinions please.
Mark
I don't personally think that Python suffers from an absence of the Switch-Case statements. The if-elif method is very similar, and though you have to repeatedly state the variable being assessed, you don't need to include break statements.
The if-elseif approach (while not so elegant) can also be applied to many other languages as well, so from an educational point of view it may be more universal.
I do not believe so and here is why.
If I had many cases to test, I can use the dictionary list and simply use that in place of a case. The IF condition would be the key with the result being the value. A very elegant piece of code because dictionary lookup is fast(binary lookup I think). BTW, I used this technique in SAS on a mainframe(old dinosaur still in use). Oops I am dating myself.
Clem
Paul
Your comment about being if / elif being used in others is a point well made! I just like the use of switch cases in other languages. But of you are going from python onto another such as c++ then you already have a structure that works. But of course going from c++ to python you may, as I have, be used to using cases. Just one of those things I suppose