Applying a decorator to a module

Terry Reedy tjreedy at udel.edu
Fri Nov 28 01:02:48 CET 2008


Decorators are syntactic sugar. They add no functionality.

@decorator
def/class possibly_long_name ...

abbreviates

def/class possibly_long_name ...
<possibly many lines running off screen or page>
possibly_long_name = decorator(possibly_long_name)

thereby warning the reader at the beginning that possibly_long_name will 
be rebound and avoiding typing possibly_long_name thrice.

Neither consideration applies to module imports.
Wrapping the functions and classed inside the module does not wrap the 
module, it mutates it.  Module names tend to be short and would only 
need to be written twice, not thrice. And the mutation call would 
immediately follow the import line, so readers can easily see what happens.




More information about the Python-list mailing list