Applying a decorator to a module
Diez B. Roggisch
deets at nospam.web.de
Thu Nov 27 16:28:54 EST 2008
lkcl schrieb:
> 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
>> modules.
>
> awWww! i'm going to quietly throw my toys out of my pram.
>
> ... but seriously - doesn't that strike people as... a slightly odd
> omission?
Decorators are applied when the function or class is *defined*. Modules
OTOH are imported from quite a few places. So what would
@foo
import bar
@baz
import bar
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)
if callable(thing):
wrapped_thing = wrap(thing)
setattr(module, name, wrappend_thing)
Diez
More information about the Python-list
mailing list