Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # WORKING CODE
- mapping = {}
- def saying(text):
- def decorator(function):
- mapping[text] = function
- return function
- return decorator
- @saying("hi")
- def hi():
- print "hello there"
- @saying("thanks")
- @saying("gracias")
- def thanks():
- print "you're welcome"
- mapping["hi"]() #running this line will print "hello there"
- mapping["thanks"]() #running this line will print "you're welcome"
- #NON-WORKING CODE:
- class politeModule(object):
- def __init__(self):
- self.mapping = {}
- @saying("hi")
- def hi(self):
- print "hello there"
- @saying("thanks")
- @saying("gracias")
- def thanks(self):
- print "you're welcome"
- module = politeModule()
- module.mapping["hi"]()
- module.mapping["thanks"]()
Add Comment
Please, Sign In to add comment