case/switch statement?

Peter Hansen peter at engcorp.com
Tue Jun 14 07:45:13 EDT 2005


D H wrote:
> Peter Hansen wrote:
>> With a case statement, on the other hand, you *know* that it must be 
>> just simple conditionals (a series of x == some_constant tests), so 
>> you don't need to look at all the cases, just the one that interests you.
> 
> Since you and Steve Holden agree that a case statement is useful, why 
> don't you propose it for python, or add it to the wiki page for Python 
> 3000.

Two simple reasons.

1. You forgot my previous comment that "in current Python the equivalent 
approach is, of course, a dictionary of some kind, though it's arguable 
whether this is as clean in many cases as a case statement would be."

2. Just because something is "useful" doesn't mean it should be added to 
Python.  The bar should be set much higher, and should include at least 
"and significantly better than any existing alternative way of doing the 
same thing."  Now go read point 1 again... ;-)

My point was not to suggest that I want a case statement in Python, nor 
even that a case statement is a good thing to have in a language (though 
it might be... it's not my place to say).  My point was simply to point 
out that performance is not the only reason to use a case statement.

-Peter



More information about the Python-list mailing list