[Tutor] Case ? (fwd)

Alan G alan.gauld at freenet.co.uk
Thu Jul 7 02:15:16 CEST 2005


The reason why Python doresn't have a case statement is simply 
because Guido didn't put one in. Why he didn't I don't know, 
you'd need to ask him. But I guess that since case stateents 
are simply syntactic sugar (well nearly), that providing one 
might have seemed like going against Python's ethos of explicit 
being better than implicit.

The reason why it doesn't have C style case statements is more 
likely to be due to the fact that C case statements are 
notoriously bug prone. Pascal or VB style case statements 
might have been acceptable...

But ultimately they are only a minor performance tweak over 
an if/elif chain saving the cost of one boolean comparison
and possibly an assembler jump per case. 

Long case constructs are better done as dispatch tables - and
yes you have to wrap them in functions, but thats definitely a 
good thing and should be done in well written case statements 
too IMHO.

Short case statements are usually fine in if/elif chains.

And type based case statements should be eliminated by using 
objects and polymorphism.

But see a long thread in the archives between Jeff Smith 
(advocating cases) and myself playing devils advocate for the 
status quo (about a year ago?) for much more discussion :-)

HTH,

Alan G.



More information about the Tutor mailing list