Guest User

Untitled

a guest
Jun 14th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. # WORKING CODE
  2. mapping = {}
  3.  
  4. def saying(text):
  5. def decorator(function):
  6. mapping[text] = function
  7. return function
  8. return decorator
  9.  
  10. @saying("hi")
  11. def hi():
  12. print "hello there"
  13.  
  14. @saying("thanks")
  15. @saying("gracias")
  16. def thanks():
  17. print "you're welcome"
  18.  
  19. mapping["hi"]() #running this line will print "hello there"
  20. mapping["thanks"]() #running this line will print "you're welcome"
  21.  
  22. #NON-WORKING CODE:
  23. class politeModule(object):
  24. def __init__(self):
  25. self.mapping = {}
  26.  
  27. @saying("hi")
  28. def hi(self):
  29. print "hello there"
  30.  
  31. @saying("thanks")
  32. @saying("gracias")
  33. def thanks(self):
  34. print "you're welcome"
  35.  
  36. module = politeModule()
  37. module.mapping["hi"]()
  38. module.mapping["thanks"]()
Add Comment
Please, Sign In to add comment