17

I have a module which contains a lot of functions (more than 25). I want to add a common decorator function to each of these functions. The normal way to do is to add a @decorator line above each function, but I was wondering if there is a better way to do it? Probably I can declare a global decorator at the top of the module or something else?

Note that since I am using someone else's code, I want to minimize the number of lines changed, so modifying the module is not ideal for me.

Thanks.

2 Answers 2

20

If your decorator is called my_decorator

### Decorate all the above functions
import types
for k,v in globals().items():
    if isinstance(v, types.FunctionType):
        globals()[k] = my_decorator(v)

You could also apply this to the module after importing it

import othermodule
import types
for k,v in vars(othermodule).items():
    if isinstance(v, types.FunctionType):
        vars(othermodule)[k] = my_decorator(v)
4
  • This is perfectly safe - and I actually use things like this in a real project.
    – jsbueno
    Jan 21, 2012 at 11:19
  • Thanks for the answer! This does work for me, but there is a small problem that it also adds the functions which I have imported (like "from copy import deepcopy") ... is there a way I can skip these functions and only add the functions that I myself defined at the top level in the module? ... thanks!
    – Rajat
    Jan 22, 2012 at 1:23
  • replace "vars(othermodule)[k] = my_decorator(v)" with "setattr(othermodule, k, my_decorator(v))" in order to avoid possible "'dictproxy' object does not support item assignment"
    – sam-6174
    May 12, 2015 at 15:48
  • Beware, though, that this makes it harder for a reader to realize that the functions are decorated. I agree sometimes it's a fine approach, but it goes a little against the "explicit is better than implicit" guideline.
    – jwd
    Mar 15, 2017 at 20:43
10

I think applying a decorator en-masse such that it's not obvious where you will go looking to find out about the function (at its definition) is generally a bad idea. Explicit is better than implicit, and all that.

If you want to apply the decorator to some third party module's functions, without modifying the third-party code, here is how I would do it:

# my_wrapper_module.py

import some_module
import functools

def some_decorator(func):
    @functools.wraps(func):
    def wrapper(*args, **kwargs):
        ...
    return wrapper

FUNCTION_NAMES = [
    'some_func_1',
    'some_func_2',
    'some_func_3',
    ...
]

for name in FUNCTION_NAMES:
    globals()[name] = some_decorator(getattr(some_module, name))

And then use these functions elsewhere by doing from my_wrapper_module import some_func_2, etc.

For me, this has the following advantages:

  1. No need to modify the third-party source file
  2. It is clear from the call site that I should go look at my_wrapper_module to see what I'm calling, and that I'm not using the undecorated versions of the functions
  3. It is clear from my_wrapper_module what functions are being exported, that they originally come from some_module, and that they all have the same decorator applied
  4. Any code that imports some_module directly isn't silently and inexplicably affected; this could be particularly important if the third-party code is more than one module

But if what you're trying to do is hack a third-party library so that internal calls are affected, then this is not what you want.

3
  • Like your idea of ​​creating wrapper_module. By the way, I've deleted my answer (because @gnibbler's is more complete and has correct cheking for function type).
    – reclosedev
    Jan 21, 2012 at 14:19
  • Thanks for the answers. I did the following
    – Rajat
    Jan 22, 2012 at 1:16
  • @reclosedev Cheers, I've removed the reference to your answer now.
    – Ben
    Jan 22, 2012 at 1:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.