February 11, 2008

Python Switch Statements - part 2

Drew Stephens @ 3:22 pm — Tags: ,

My friend Lex read my post on Python’s lack of switch statements and sent me a note that the normal method for implementing something along the lines of a switch in Python is to use a dictionary. First, let’s define some functions:

def fooFunc():
    print 'Got foo?'
def barFunc():
    print 'Not foo.'
def nomatch():
    print 'No function to speak of!'
 
string = 'foo'

Now we can create a dictionary and just lookup the variable you want to switch upon:

# Make a dictionary and call it switch
switch = {
    'foo': fooFunc,
    'bar': barFunc
    }
 
# Find the string in the dictionary, thereby calling the function
switch[string]()
switch['bar']()
 
# Another way to do the dictionary lookup
switch.get( string, nomatch )()
switch.get( 'bar', nomatch )()

Or create an anonymous dictionary and do it all at once, more like a traditional switch:

{ 'foo' : fooFunc,
'bar' : barFunc }[string]()

Related Posts:

3 Comments »

  1. I’ve been using python extensively for the last couple of months for an independent study project, and I have to say - it isn’t my favorite language.

    Some things like excluding switch statements are just annoying, as are the iterators that don’t contain a reference to the previous element in a structure. Others, like not doing implicit casting when trying to print, are just stupid. When I print a variable, I don’t want to have to first cast it to a string. Even worse, having to call str(obj) instead of obj.str() just removes from the OO feel of the language. It is kind of icky. Yes, all these issues can be worked around if you apply your programming knowledge to work around the language, but languages are supposed to be tools. I don’t want to have to add extra steps when I’m writing code just to bypass a language’s refusal to implement things that I’ve come to expect as standard. (Enums are another example of something I sorely miss in python, a fake implementation of which also uses a dictionary.)

    I know python’s mantra is “explicit not implicit,” but I like the syntactic sugar of ruby and even the ease in which perl allows you to do things without laying everything out. Working with python has just felt kludgy, and I’m not sure what all the hype is about.

    Just my $.02 ;)

    [Reply]

    Comment by Robert Peaslee — February 11, 2008 @ 3:56 pm

  2. Maybe it’s because I come from Perl, where we don’t have switch statements either (kudos if you’re already running 5.10 everywhere) but their omission doesn’t really bother me.

    I do, however, feel your pain in the lacking iterators and the breaking of OO conventions in a few aspects of Python. Even though it sounds cliché, the whole whitespace as structure thing is quite annoying, too. Kludgy does sound a bit right; though new languages always begin by being frustrating due to a lack of knowledge and then go on to be new and interesting, Python has returned back to frustrating for me, because of things similar to what you mentioned.

    [Reply]

    Comment by Drew Stephens — February 12, 2008 @ 1:43 pm

  3. I agree. I keep finding things that I hate about Python. And you can use the (rather hackish) “foreach.. do && {}” structure in Perl as a mostly-reasonable and well-documented work-around in Perl. And, as noted above, even this is no longer necessary with more recent Perl’s.

    Not that I think Perl (or any other languages) is free of language demons, but omigod: why on earth must i write a whole separate function for a switch-step??? I really believe too much boiler-plate destroys the flow and comprehension of a program. I’m sure people honestly believe that this just aids debugging. I say Rubbish: this looks a lot more like a dictatorial hack because it was easier to push the work back to every python developer instead of making the language help do that work for you.

    [Reply]

    Comment by Will — December 8, 2008 @ 11:04 am

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress