Python program start

前端 未结 4 806
不思量自难忘°
不思量自难忘° 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:36

    A better pattern is this:

    def main():
       ...
    
    if __name__ == '__main__':
       main()
    

    This allows your code to be invoked by someone who imported it, while also making programs such as pychecker and pylint work.

提交回复
热议问题