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

Ian Kelly ian.g.kelly at gmail.com
Wed Nov 12 23:19:09 CET 2014


On Wed, Nov 12, 2014 at 3:09 PM, Chris Kaynor <ckaynor at zindagigames.com> wrote:
> I was thinking along the lines of replacing:
>
> if __name__ == "__main__":
>     <<<block of code>>>
>
> with
>
> @main
> def myFunction()
>     <<<<block of code>>
>
> Both blocks of code will be called at the same time.

99% of the time the content of <<<block of code>>> is just "main()",
so then you're proposing replacing this:

if __name__ == "__main__":
    main()

with this:

@main
def myFunction():
    my_main()

Which feels redundant to me. Why have a function here that does
nothing but call another function?

I think if this is the goal then a simple predicate would be clearer:

if is_main_module():
    main()



More information about the Python-list mailing list