Created
May 16, 2012 05:59
-
-
Save jerith/2707900 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Toy(object): | |
def handle_input(self, text): | |
command, rest = (text.split(None, 1) + [''])[:2] | |
return getattr(self, 'cmd_' + command, self.unknown_command)(command, rest) | |
def cmd_foo(self, command, rest): | |
print "Foo! %s" % (rest,) | |
def cmd_rev(self, command, rest): | |
print ''.join(reversed(rest)) | |
def unknown_command(self, command, rest): | |
print "Unknown command: %s" % (command,) | |
toy = Toy() | |
toy.handle_input("foo some stuff") | |
toy.handle_input("rev some stuff") | |
toy.handle_input("bar some stuff") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment