Python Switch Statements - part 2

| 4 Comments | No TrackBacks

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)()

Which will call the function in the switch dictionary, or call nomatch if none of the dictionary entries match. Or create an anonymous dictionary and do it all at once, more like a traditional switch:

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

No TrackBacks

TrackBack URL: http://dinomite.net/cgi-bin/mt/mt-tb.cgi/214

4 Comments

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 ;)

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.

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.

Well I agree that the lack of a switch construct does lead to some inconvenience however I feel that it's a matter of getting used to doing things in a new programming language. I don't agree that this is a dictatorial hack meant to get away with doing more work as some of the features in Python are definitely harder to implement than a switch construct. The reason for its omission is more likely due to certain linguistic concerns or something. By the way I am in support of a traditional switch construct just that I don't agree with th statement that the good people working on Python is simply trying to get away with more work by not implementing it.

Leave a comment

Pages

About this Entry

This page contains a single entry by Drew Stephens published on February 11, 2008 12:22 PM.

Off The Hook - 6 February 2008 was the previous entry in this blog.

Off The Hook - 13 February 2008 is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.