How about some syntactic sugar for " __name__ == '__main__' "?

Skip Montanaro skip.montanaro at gmail.com
Wed Nov 12 22:55:04 CET 2014


> def main(func):
>     if func.__module__ == "__main__":
>             func()
>     return func # The return could be omitted to block the function from
> being manually called after import.
>
> Just decorate the "main" function of the script with that, and it will be
> automatically called when ran as a script, but not when imported as a
> module.

This won't work (I don't think) if you want to call the "main"
function from another place (like the interpreter prompt).

Skip



More information about the Python-list mailing list