On 16/03/2016 04:06, Mario R. Osorio wrote:
On Tuesday, March 15, 2016 at 9:55:27 PM UTC-4, jj0ge...@gmail.com wrote:
You have apparently mistaken me for someone who's worried. I don't use Python, I was
just curious as to why a construct that is found, not only to be useful in 95% of other
languages, but is generally considered more flexible and readable than the if-elif, was
missing in Python. (your link "Switch Statement Code Smell" not withstanding)
Have a great day :)
Switch and case statements are such a waste of time that, in order to
understand them you have to mentally use if/elseif/else/endif statements.
Furthermore, the concepts of switch and case could not even exist without the
notion of if/elseif/else/endif statements. Why then, add an extra level of
complication??.
You're understanding them wrong then. Switch can be used like a
'computed go to', where you execute the nth statement of a selection, or
select the nth value of a list /without needing to evaluate any of the
others/.
Or it can be more general, where each statement or value in the
selection has a corresponding 'case', and it will directly execute or
select it according to the switch expression:
switch d:
case 1:
n = p//8
pal = 2
case 4:
n = p//2
pal=16
case 8:
n = p
pal = 256
case 16:
n = p*2
case 24:
n = p*3
case 32:
n = p*4
a = 1
else:
raise ...
Sure, there are half a dozen ways of writing such code. But you don't
want to think of it in terms if-elif where you first have to test all
the other cases when d is 32, and have to keep writing 'd==' over and over.
And you don't want to take this straightforward bit of code and spread
it over many functions and classes and methods; it's all here.
(BTW why does Python have 'elif' when if-else is all that is really needed?)
Go play with Java or Maybe C++. Have fun with their fancy if/elseif/else/endif
statements oops, sorry, they call them switch and/or case statements.
Wish you the best and please lete us know if and when you have a deep fall
through a breakeless case ...
C got those badly wrong. (It got a lot of things wrong.) That doesn't
mean that switch, properly implemented, is bad.
--
Bartc
--
https://mail.python.org/mailman/listinfo/python-list