Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MyClass:
- def call(self, name):
- print "Executing function:", name
- getattr(self, name)()
- def my_decorator(some_function):
- def wrapper():
- print("Before we call the function.")
- some_function()
- print("After we call the function.")
- return wrapper
- @my_decorator
- def my_function(self):
- print "My function is called here."
- engine = MyClass()
- engine.call('my_function')
- class MyClass:
- def call(self, name):
- print "Executing function:", name
- getattr(self, name)()
- def my_decorator(some_function):
- def wrapper():
- print("Before we call the function.")
- some_function()
- print("After we call the function.")
- return wrapper
- # @my_decorator
- def my_function(self):
- print "My function is called here."
- engine = MyClass()
- engine.call('my_function')
- >>> engine.my_function()
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- TypeError: 'NoneType' object is not callable
- >>> MyClass.my_function is None
- True
- def my_decorator(some_function):
- def wrapper(self):
- print("Before we call the function.")
- # some_function is no longer bound, so pass in `self` explicitly
- some_function(self)
- print("After we call the function.")
- # return the replacement function
- return wrapper
- class MyClass:
- def call(self, name, *args, **kwargs):
- print("Executing function: {!r}".format(name))
- getattr(self, name)(*args, **kwargs)
- def my_decorator(some_function):
- def wrapper(self, *args, **kwargs):
- print("Before we call the function.")
- some_function(self, *args, **kwargs)
- print("After we call the function.")
- return wrapper
- @my_decorator
- def my_function(self):
- print("My function is called here.")
- del my_decorator # Not a permanent part of class.
- engine = MyClass()
- engine.call('my_function')
Add Comment
Please, Sign In to add comment