Decorators¶
Legacy Wiki Page
This page was migrated from the old MoinMoin-based wiki. Information may be outdated or no longer applicable. For current documentation, see python.org.
See also PythonDecoratorLibrary. And apart from decorators in the standard library, there are also lots of PyPI packages with convenient, interesting or novel use cases:
(Note: Not sure this is going anywhere. Relisting builtins is somewhat redundant, but on topic here. The focus is novel/interesting decorators in the wild. The categorization probably won’t hold up; and probably going to split this up into sections.)
Other decorator links¶
PyPI decorator packages (gets interesting around page 10)
__main__¶
This decorator does not alter a function, but causes it to be executed if __name__ == '__main__'{.backtick}. This provides an experimental cleaner syntax to the traditional way of bootstrapping a python script. This should not be used on class methods.
The function gets a copied list of sys.argv arguments.
1 def __main__(func):
2
3 if __name__ == "__main__":
4 import sys, os
5 args = sys.argv[:]
6 args[0] = os.path.abspath(args[0])
7 func(args)