Skip to content Skip to sidebar Skip to footer

Apply A Single Decorator To Multiple Functions

I've searched for this, but the results I've seen involve the opposite: applying multiple decorators to a single function. I'd like to simplify this pattern. Is there a way to app

Solution 1:

If you want to make the "long" function call only once and decorate all three functions with the result, just do exactly that.

my_patch = patch('somelongmodulename.somelongmodulefunction')

@my_patchdeftest_a(patched):
    pass@my_patchdeftest_b(patched):
    pass

Solution 2:

If you want to get fancy, you can modify globals(). The general idea below should work.

test_pattern = re.compile(r'test_\w+')
test_names = [name for name inglobals().keys() if test_pattern.match(name)]
for name in test_names:
  globals()[name] = decorate(globals()[name])

Post a Comment for "Apply A Single Decorator To Multiple Functions"