Should I use a main() method in a simple Python script?

后端 未结 5 2160
Happy的楠姐
Happy的楠姐 2020-12-24 06:52

I have a lot of simple scripts that calculate some stuff or so. They consist of just a single module.

Should I write main methods for them and call them with the

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-24 07:22

    The if __name__ construct will let you easily re-use the functions and classes in the module in other Python scripts. If you don't do that, then everything in the module will run when it is imported.

    If there's nothing in the script you want to reuse, then sure, just dump it all in there. I do that sometimes. If you later decide you want to reuse some code, I have found Python to be just about the easiest language in which to refactor code without breaking it.

提交回复
热议问题