Skip to content

Instantly share code, notes, and snippets.

@jerith
Created May 16, 2012 05:59
Show Gist options
  • Save jerith/2707900 to your computer and use it in GitHub Desktop.
Save jerith/2707900 to your computer and use it in GitHub Desktop.
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