CONSTRUCT - Module Attributes and Execution Environment

lazaridis_com ilias at lazaridis.com
Fri Aug 25 13:20:37 EDT 2006


Duncan Booth wrote:
> lazaridis_com wrote:
>
> > Are ther alternative constructs/mechanism available, which could be
> > used to add this functionality possiby directly to a code-module?
>
> How about something along these lines:
>
> ------------------ auto.py ---------
> import sys, atexit
>
> def main_body(f):
>     if f.func_globals['__name__']=='__main__':
>         atexit.register(f, sys.argv)
>     return f
>
> @main_body
> def auto(args):
>     print "auto run", args
> ------------------------------------
>
> If you run auto.py as a script then the decorated function executes. If you
> import it then the decorated function doesn't execute. In your own script
> you just need an import statement and to put the decorator on your main
> function.

This construct looks very promising !

Are function decorators available in Ruby / Java / C++ ?




More information about the Python-list mailing list