Applying a decorator to a module
Diez B. Roggisch
deets at nospam.web.de
Thu Nov 27 22:28:54 CET 2008
> On Nov 27, 7:43 pm, s... at pobox.com wrote:
>> lkcl> Very simple question: how do you apply a decorator to an entire
>> lkcl> module?
>> Function-by-function or class-by-class. There is no decorator support for
> awWww! i'm going to quietly throw my toys out of my pram.
> ... but seriously - doesn't that strike people as... a slightly odd
Decorators are applied when the function or class is *defined*. Modules
OTOH are imported from quite a few places. So what would
mean - what's the semantics of that? If *anything*, you could declare a
decorator per module.
Anyway, for your original problem, you can always just iterate over the
module objects & decorate them. Like this:
for name in dir(module):
thing = getattr(module, name)
wrapped_thing = wrap(thing)
setattr(module, name, wrappend_thing)
More information about the Python-list