wrapper

Python Wrap Class Method

Python Wrap Class Method Question: I’m trying to create an object with a run method that will be wrapped by a _wrap_run method. I’d like to be able to call the method and it’s wrapper by simply typing instance.run() and I’d like to be able to subclass the object so I can override the run() …

Total answers: 5

Making decorators with optional arguments

Making decorators with optional arguments Question: from functools import wraps def foo_register(method_name=None): “””Does stuff.””” def decorator(method): if method_name is None: method.gw_method = method.__name__ else: method.gw_method = method_name @wraps(method) def wrapper(*args, **kwargs): method(*args, **kwargs) return wrapper return decorator Example: The following decorates my_function with foo_register instead of ever making it to decorator. @foo_register def my_function(): print(‘hi…’) …

Total answers: 14

python parent class 'wrapping' child-class methods

python parent class 'wrapping' child-class methods Question: I have the following situation in my python code: class Parent(object): def run(self): print “preparing for run” self.runImpl() print “run done” class Child(Parent): def runImpl(self): print “child running” However, I have cases with several generations of such ‘decorators’, doing different setup/teardown steps before and after ‘runImpl’, and currently …

Total answers: 4

PHP equivalent for a python decorator?

PHP equivalent for a python decorator? Question: I want to be able to wrap a PHP function by another function, but leaving its original name/parameter list intact. For instance: function A() { print “inside A()n”; } function Wrap_A() { print “Calling A()n”; A(); print “Finished calling A()n”; } // <— Do some magic here (effectively …

Total answers: 6