Python program start

前端 未结 4 804
不思量自难忘°
不思量自难忘° 2021-02-05 05:33

Should I start a Python program with:

if__name__ == \'__main__\':
some code...

And if so, why? I saw it many times but don\'t have a clue about

4条回答
  •  太阳男子
    2021-02-05 06:33

    This is good practice. First, it clearly marks your module entry point (assuming you don't have any other executable code at toplevel - yuck). Second, it makes your module importable by other modules without executing, which some tools like code checkers, packagers etc. need to do.

提交回复
热议问题