class Dispatcher: def __init__(self): self._run() def cmd1(self): print('I\'m cmd1') def cmd2(self): print('I\'m cmd2') def _run(self): while True: cmd = input("plz input a command:").strip() if cmd == 'quit': break getattr(self,cmd,lambda :print('unknown command {}'.format(cmd)))() Dispatcher()