Applying a decorator to a module

skip at pobox.com skip at pobox.com
Thu Nov 27 15:14:02 EST 2008


    >> Function-by-function or class-by-class.  There is no decorator
    >> support for modules.

    lkcl> ... but seriously - doesn't that strike people as... a slightly
    lkcl> odd omission?

Decorators are still a new feature in the language and were purposely added
in an incremental fashion.  In 2.5 they were added for functions.  Suitable
use cases were found to apply them to classes in 2.6.  You could post your
needs to the python-ideas at python.org list and see if they gain any traction
there.  If so, perhaps decorators for modules can be added for 2.7.

One obvious question about module decorators would be where to apply them.
In your original post you suggested perhaps applying it at import time:

    @compiletojs
    import mypyjamasmodule

What if somewhere else in your application you had a naked import:

    import mypyjamasmodule

Should that be compiled into normal Python bytecode?

I suspect in most instances you'd want a module compiled one way or the
other.  To avoid errors you probably want the decorator inside the module
(somewhere - at the top?).  Unfortunately, there is no keyword upon which
you can hang a decorator as you can with functions (def) and classes
(class).  import is not the same as a definition.

It's not obvious to me that module decorators would use precisely the same
sort of syntax as function and class decorators.

Skip




More information about the Python-list mailing list