hong zhang <henryzhang62 <at> yahoo.com> writes: > > List, > > Python does not have switch statement. Any other option does similar work? > Thanks for help. > > --henry > >
I see a couple of people have mentioned using a dictionary. If the value that you are switching on is a string, or could be made into one, you can use a variant of the command dispatch pattern. class MyCommandDispatcher(object): def do_a(self): # do stuff def do_b(self): # do stuff def do_5(self): # do stuff def default(self): # do stuff def switch(self, option): getattr(self, 'do_' + str(option), self.default)() d = MyCommandDispatcher() d.switch('a') d.switch(5) This isn't _much_ more coding than using the dictionary method, and is pretty readable. This is also a common pattern in python. Matt -- http://mail.python.org/mailman/listinfo/python-list