Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old March 5th, 2004, 01:51 PM
XxChris XxChris is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 217 XxChris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
"if" Alternative

I'm parsing an inputed string for commands, and I was wondering if there is a better way to associate the inputed commands with their action than just using ifs

i.e.
Code:
if command == 'foo':
        doStuff()
if command == 'bar':
        foo = 0
        doMoreStuff()

...

Reply With Quote
  #2  
Old March 5th, 2004, 05:33 PM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,196 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 5 Days 13 h 42 m 7 sec
Reputation Power: 252
Yes there is - use a dispatch table. e.g.

Code:
dispatchTable = { 
    'foo' : doStuff,
    'bar' : doMoreStuff,
    #etc
}

dispatchTable[command]()


This does have some limitations - each of your actions must be packed into separate functions. In your original code the 'bar' command sets foo=0 before the call to doMoreStuff, which would not be possible using this technique.

Sadly Python does not have anonymous code blocks, which would make this technique more powerful. You could possibly simulate it using eval and the compiler module, but it would probably be more trouble than it is worth.

Regards,

Dave - The Developers' Coach

Reply With Quote
  #3  
Old March 5th, 2004, 11:19 PM
lazy_yogi lazy_yogi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 325 lazy_yogi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 58 m 36 sec
Reputation Power: 6
Dave's way is the python way alternative to the switch statement.

If you're looking for switch, it was intentionally left out of python for the same reasons as removing +=,-=,etc .. as well as leaving out a host of other things such as condition?ret_true:ret_false.

Python maintains the highest clarity (as far as code readability goes) as well as removing possible causes of bugs. Alot of ppl forget about the break in switch statements which causes alot of bugs. Also if/elif/else is the clearest code for the job as far as readability goes..

Reply With Quote
  #4  
Old March 5th, 2004, 11:31 PM
XxChris XxChris is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 217 XxChris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
It's it just me or do '+=' and '-=' work? I'm guessing you mean the increment and decrement operators (++ and --).

Reply With Quote
  #5  
Old March 6th, 2004, 01:56 AM
lazy_yogi lazy_yogi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 325 lazy_yogi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 58 m 36 sec
Reputation Power: 6
ah crap .. yeh that's wot I meant =P

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > "if" Alternative


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway