GIDForums  

Go Back   GIDForums > Computer Programming Forums > Python Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-May-2006, 02:07
crystalattice's Avatar
crystalattice crystalattice is offline
Aspiring author
 
Join Date: Apr 2004
Location: Japan (again)
Posts: 1,635
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice
Thumbs up

Making a "switch" function


For those who don't know, Python does not have a switch() function, unlike many other languages. Instead you have to use if/elif statements, like so:
Python Code:
if x == 1:
    print "one"
elif x ==2:
    print "two"
else:
    print "three"

However, I just stumbled upon a site (http://www.voidspace.org.uk/python/articles/OOP.shtml) that gives a nifty way of making a switch() style function using dictionaries. It's slightly more complicated than using if/elif statements, but it may make more sense if you have a lot of options. Which way is better is up to you to decide.
Python Code:
def function1():
    print 'You chose one.'
def  function2():
    print 'You chose two.'
def  function3():
    print 'You chose three.'
#
# switch is our dictionary of functions
switch = {
    'one': function1,
    'two': function2,
    'three': function3,
    }
#
# choice can eithe be 'one', 'two', or 'three'
choice = raw_input('Enter one, two, or three :')
#
# call one of the functions
try:
    result = switch[choice]
except KeyError:
    print 'I didn\'t understand your choice.'
else:
    result()
__________________
Start Programming with Python-A beginner's guide to programming and the Python language.
-------------
Common Sense v2.0-Striving to make the world a little bit smarter.
  #2  
Old 08-May-2006, 02:36
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,031
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Re: Making a "switch" function


I had somewhat similar problems in C++ with string elements : you can't use a switch statement on strings! When configuring an application with XML files, making programmatic decisions based on string comparison can make sense.
The solution was to use maps. I had to add each string element to the map, in a similar way you use the dictionary. Tying the string to an action is more complicated however, and even without such a requirement, the Python code looks much nicer!

Thanks,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #3  
Old 09-May-2006, 14:48
crystalattice's Avatar
crystalattice crystalattice is offline
Aspiring author
 
Join Date: Apr 2004
Location: Japan (again)
Posts: 1,635
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice

Re: Making a "switch" function


I remember in my C++ class having a problem that had me trying to use strings with a switch(). I didn't know about maps at the time so I kludged a similar way to do it by using integers as the switch() argument, then tying the number to a string.

It probably wasn't as "elegant" as a map, but I agree that the Python way is easier.
__________________
Start Programming with Python-A beginner's guide to programming and the Python language.
-------------
Common Sense v2.0-Striving to make the world a little bit smarter.
  #4  
Old 18-Oct-2006, 15:11
Ene Uran Ene Uran is offline
New Member
 
Join Date: Oct 2006
Location: USA West
Posts: 8
Ene Uran is on a distinguished road

Re: Making a "switch" function


Quote:
Originally Posted by crystalattice
For those who don't know, Python does not have a switch() function, unlike many other languages. Instead you have to use if/elif statements, like so:
Python Code:
if x == 1:
    print "one"
elif x ==2:
    print "two"
else:
    print "three"

However, I just stumbled upon a site (http://www.voidspace.org.uk/python/articles/OOP.shtml) that gives a nifty way of making a switch() style function using dictionaries. It's slightly more complicated than using if/elif statements, but it may make more sense if you have a lot of options. Which way is better is up to you to decide.
Python Code:
def function1():
    print 'You chose one.'
def  function2():
    print 'You chose two.'
def  function3():
    print 'You chose three.'
#
# switch is our dictionary of functions
switch = {
    'one': function1,
    'two': function2,
    'three': function3,
    }
#
# choice can eithe be 'one', 'two', or 'three'
choice = raw_input('Enter one, two, or three :')
#
# call one of the functions
try:
    result = switch[choice]
except KeyError:
    print 'I didn\'t understand your choice.'
else:
    result()
Even in C++ the switch/case construct has slower performance than the "if/else if" construct. I would stick with if/elif in Python, particularly if your functions carry arguments, than the switch dictionary becomes rather tough.
  #5  
Old 19-Oct-2006, 13:03
crystalattice's Avatar
crystalattice crystalattice is offline
Aspiring author
 
Join Date: Apr 2004
Location: Japan (again)
Posts: 1,635
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice

Re: Making a "switch" function


The if/elif construct is actually really easy to use and it makes sense. I've never tried the above switch dictionary because it's a lot more work to get the same thing.

I wasn't aware of the speed difference in C++ though. How bad can the performance hit be, assuming worst case scenarios?
__________________
Start Programming with Python-A beginner's guide to programming and the Python language.
-------------
Common Sense v2.0-Striving to make the world a little bit smarter.
 
 

Recent GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 14:12
[Tutorial] Function Pointers aaroncohn C++ Forum 4 17-Feb-2006 12:33
[GIM] gim.h dsmith C Programming Language 0 18-Jan-2005 09:48
Help making a function darkzerox C Programming Language 4 23-May-2004 14:55
Revising Script style ?????? pepee MySQL / PHP Forum 4 14-Apr-2004 05:59

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:15.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.