fork download
  1. class Foo:
  2. def __call__(self, event_name, *args, **kwargs):
  3. fn = getattr(self, 'handle_{}'.format(event_name))
  4. return fn(*args, **kwargs)
  5.  
  6. def handle_foo(self):
  7. print('foo')
  8.  
  9. def handle_bar(self, x):
  10. print('bar', x)
  11.  
  12. dispatch = Foo()
  13. dispatch('foo')
  14. dispatch('bar', 42)
  15. dispatch('whatever')
Runtime error #stdin #stdout #stderr 0.02s 9936KB
stdin
Standard input is empty
stdout
foo
bar 42
stderr
Traceback (most recent call last):
  File "./prog.py", line 15, in <module>
  File "./prog.py", line 3, in __call__
AttributeError: 'Foo' object has no attribute 'handle_whatever'