简体   繁体   English

我必须在我的python代码中有main函数吗?

[英]do I have to have main function in my python code?

Can I write a python code with a bunch of functions but without a main function. 我可以用一堆函数编写一个python代码但没有main函数。 The purpose of this script is for other scripts to import some of the functions from. 此脚本的目的是为其他脚本导入一些函数。 I will call it setvar_general.py or something which will be imported by a series of other setvar_x scripts. 我将其称为setvar_general.py或将由一系列其他setvar_x脚本导入的内容。 While these setvar_x do more specific things, setvar_general does not do any thing other than providing building blocks. 虽然这些setvar_x执行更具体的操作,但setvar_general除了提供构建块之外不会做任何事情。 Therefore there is not need for defining a main function in setvar_general.py. 因此,不需要在setvar_general.py中定义main函数。

I guess it all comes down to the question "do I have to have main function"? 我想这一切都归结为“我必须拥有主要功能”的问题吗?

You do not have to have a main function in Python and writing separate files without a main function, to be imported into other programs, is the normal and correct way of doing Python programming. 您不必在Python中使用main函数并且在没有 main函数的情况下编写单独的文件,以便导入到其他程序中,这是执行Python编程的正常和正确的方法。

When a Python file is loaded (either using import or by getting executed from the command line) each statement in the program is executed at that time . 当一个Python文件被加载(使用import或通过在命令行获取执行)程序中的每个语句在该时间执行。 Statements that are def or class statements create a function or class definition for later use. defclass语句的语句创建函数或类定义供以后使用。 Statements that are are not inside a def or class will be executed right away. 不在defclass语句将立即执行。

Therefore, the equivalent of a main() function in other languages is actually the set of executable statements found in your file. 因此,在其他语言中等效的main()函数实际上是在您的文件中找到的可执行语句集。 If you limit these to def and/or class statements, you will get the effect you want. 如果将这些限制为def和/或class语句,您将获得所需的效果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我在 python 中编写了多个函数的代码。 我正在对我的代码进行单元测试,但主要的 function 正在重复自己 - I have written a code in python of multiple functions. I'm unit testing my code but the main function is repeating itself 我的Python代码上有多少个线程? - How many threads do I have on my Python code? 如何让主 function 使用另一个 function? - How do I have the main function use another function? 无法将浏览的文件名复制到主程序中。 我在哪里可以编写我的主要功能,我必须在哪里编写代码。 到底在哪里? - Cannot copy the filename browsed into the main program. Where can i write my main function where i have to write my piece of code. Exactly where? 如何将 3 个数字与在 Python 中仅采用 2 个变量的函数与我编写的代码进行比较 - How do I compare 3 numbers with a function that takes only 2 variables in Python with a code that I have written Django 中的 url() function 已被弃用 - 我必须更改我的源代码吗? - The url() function in Django has been deprecated - Do I have to change my source code? 为什么我的文件代码出错? - Why do I have an error on my code with my files? 如何在 python 中为 int 和 string 类型调用 function。 我附上了我的代码片段 - How to call a function for both int and string type in python. I have attached the snippets of my code 我是否需要在我的机器上安装 Python 才能在虚拟环境中运行代码? - Do I need to have Python installed on my machine to run a code in a virtual environment? 我的 Python 代码出现数学域错误,不知道该怎么做 - I'm getting a math domain error in my Python code and have no clue what to do
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM