Guest User

Untitled

a guest
Mar 9th, 2023
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.38 KB | None | 0 0
  1. handlers = []
  2.  
  3. def register(func, data_type):
  4.     handlers.append((func, data_type))
  5.  
  6.  
  7. def handle(data):
  8.     for func, data_type in handlers:
  9.         d = data.get(func, data_type())
  10.         data[func] = func(d)
  11.  
  12.  
  13. def counter(x):
  14.     print('counter', x)
  15.     return x + 1
  16.  
  17.  
  18. register(counter, int)
  19. register(lambda s: s + 'a', str)
  20.  
  21. data = {}
  22. handle(data)
  23. handle(data)
  24. handle(data)
  25. print(data)
Advertisement
Add Comment
Please, Sign In to add comment