CONSTRUCT - Module Attributes and Execution Environment

Duncan Booth duncan.booth at invalid.invalid
Tue Aug 22 10:02:48 EDT 2006


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.



More information about the Python-list mailing list