Python,如果__name__ == __main__用代码示例解释
When a Python interpreter reads a Python file, it first sets a few special variables. Then it executes the code from the file.
当Python解释器读取Python文件时,它首先设置一些特殊变量。 然后,它执行文件中的代码。
One of those variables is called
__name__.
这些变量之一称为
__name__。
If you follow this article step-by-step and read its code snippets, you will learn how to use
if __name__ == "__main__", and why it's so important.
如果循序渐进地阅读了本文,并阅读了其代码片段,您将学习
if __name__ == "__main__",以及为什么它如此重要。
Python模块介绍 (Python Modules Explained)
Python files are called modules and they are identified by the
.pyfile extension. A module can define functions, classes, and variables.
Python文件称为模块,它们由
.py文件扩展名标识。 模块可以定义函数,类和变量。
So when the interpreter runs a module, the
__name__variable will be set as
__main__if the module that is being run is the main program.
因此,当解释器运行模块时,如果正在运行的模块是
__name__,则
__name__变量将设置为
__main__。
But if the code is importing the module from another module, then the
__name__variable will be set to that module’s name.
但是,如果代码从另一个模块导入该模块,则
__name__变量将设置为该模块的名称。
Let's take a look at an example. Create a Python module named
file_one.pyand paste this top level code inside:
让我们看一个例子。 创建一个名为
file_one.py的Python模块,并将以下顶级代码粘贴到其中:
By running this file you will see exactly what we were talking about. The variable
__name__for this module is set to
__main__:
通过运行此文件,您将确切了解我们在说什么。 此模块的变量
__name__设置为
__main__:
File one __name__ is set to: __main__
Now add another file named
file_two.pyand paste this code inside:
现在添加另一个名为
file_two.py文件,并将此代码粘贴到其中:
Also, modify the code in
file_one.pylike this so we import the
file_twomodule:
另外,像这样修改
file_one.py的代码,以便我们导入
file_two模块:
Running our
file_onecode once again will show that the
__name__variable in the
file_onedid not change, and still remains set to
__main__. But now the variable
__name__in
file_twois set as its module name, hence
file_two.
运行我们
file_one再次代码将表明
__name__的变量
file_one没有改变,仍然设置为
__main__。 但现在的变量
__name__在
file_two设置为它的模块名称,因此
file_two。
The result should look like this:
结果应如下所示:
File two __name__ is set to: file_two File one __name__ is set to: __main__
But run
file_twodirectly and you will see that its name is set to
__main__:
但是直接运行
file_two,您将看到其名称设置为
__main__:
File two __name__ is set to: __main__
The variable
__name__for the file/module that is run will be always
__main__. But the
__name__variable for all other modules that are being imported will be set to their module's name.
用于运行的文件/模块的变量
__name__始终为
__main__。 但是,正在导入的所有其他模块的
__name__变量将被设置为其模块的名称。
Python文件命名约定 (Python File Naming Conventions)
The usual way of using
__name__and
__main__looks like this:
使用
__name__和
__main__的通常方法如下所示:
if __name__ == "__main__": Do something here
Let's see how this works in real life, and how to actually use these variables.
让我们看看它在现实生活中是如何工作的,以及如何实际使用这些变量。
Modify
file_oneand
file_twoto look like this:
修改
file_one和
file_two如下所示:
file_one:
file_one:
file_two:
file_two:
Again, when running
file_oneyou will see that the program recognized which of these two modules is
__main__and executed the code according to our first
if elsestatements.
同样,在运行
file_one您将看到程序识别出这两个模块中的哪个是
__main__并根据我们的第一个
if else语句执行了代码。
The result should look like this:
结果应如下所示:
File two __name__ is set to: file_two File two executed when imported File one __name__ is set to: __main__ File one executed when ran directly
Now run
file_twoand you will see that the
__name__variable is set to
__main__:
现在运行
file_two,您将看到
__name__变量设置为
__main__:
File two __name__ is set to: __main__ File two executed when ran directly
When modules like this are being imported and run, their functions will be imported, and top level code executed.
当这样的模块被导入并运行时,它们的功能将被导入,并执行顶层代码。
To see this process in action, modify your files to look like this:
要查看此过程的实际效果,请将文件修改为如下所示:
file_one:
file_one:
file_two:
file_two:
# Python module to import print("File two __name__ is set to: {}" .format(__name__)) def function_three(): print("Function three is executed") if __name__ == "__main__": print("File two executed when ran directly") else: print("File two executed when imported")
Now the functions are loaded but not run.
现在,功能已加载但无法运行。
To run one of these functions modify the
if __name__ == "__main__"part of
file_oneto look like this:
要运行这些功能的一个修改
if __name__ == "__main__"的一部分
file_one看起来像这样:
if __name__ == "__main__": print("File one executed when ran directly") function_two() else: print("File one executed when imported")
When running
file_oneyou should see should be like this:
运行
file_one您应该看到应该是这样的:
File two __name__ is set to: file_two File two executed when imported File one __name__ is set to: __main__ File one executed when ran directly Function two is executed
Also, you can run functions from imported files. To do that, modify the
if __name__ == “__main__”part of
file_oneto look like this:
另外,您可以从导入的文件运行功能。 要做到这一点,修改
if __name__ == “__main__”的一部分
file_one看起来像这样:
if __name__ == "__main__": print("File one executed when ran directly") function_two() file_two.function_three() else: print("File one executed when imported")
And you can expect a result like this:
您可以期望这样的结果:
File two __name__ is set to: file_two_step_3 File two executed when imported File one __name__ is set to: __main__ File one executed when ran directly Function two is executed Function three is executed
Now let's say the
file_twomodule is really big with lot of functions (two in our case), and you don't want to import all of them. Modify
file_twoto look like this:
现在,假设
file_two模块确实具有很多功能(在我们的示例中为两个)非常大,并且您不想导入所有这些功能。 修改
file_two如下所示:
And to import the specific functions from the module, use the
fromimport block in the
file_onefile:
要从模块导入特定功能,请使用
file_one文件中的
fromimport块:
结论 (Conclusion)
There is a really nice use case for the
__name__variable, whether you want a file that can be run as the main program or imported by other modules. We can use an
if __name__ == "__main__"block to allow or prevent parts of code from being run when the modules are imported.
__name__变量有一个非常不错的用例,无论您想要一个可以作为主程序运行还是可以由其他模块导入的文件。 我们可以使用
if __name__ == "__main__"块来允许或阻止在导入模块时运行部分代码。
When the Python interpreter reads a file, the
__name__variable is set as
__main__if the module being run, or as the module's name if it is imported. Reading the file executes all top level code, but not functions and classes (since they will only get imported).
当Python解释器读取文件时,如果正在运行模块,则将
__name__变量设置为
__main__如果导入了模块,则将其设置为模块的名称。 读取文件将执行所有顶级代码,但不会执行函数和类(因为它们只会被导入)。
Bra gjort! (That means "Well done" in Swedish!)
胸罩gjort! (这在瑞典语中表示“做得很好”!)
Check out more articles like this on my freeCodeCamp profile, Medium profile, and other fun stuff I build on my GitHub page.
在我的freeCodeCamp配置文件 , 中型配置文件以及我在GitHub页面上构建的其他有趣的东西中查看更多类似的文章。
翻译自: https://www.freecodecamp.org/news/if-name-main-python-example/
- python 示例:解释__name__ = "__main__"
- PYTHON里if __name__ == "__main__":解释
- 【Python】|(转)if __name__ == "__main__"详细解释
- python中if __name__ == "__main__"的解释
- 解释关于python中if __name__ == '__main__':
- 关于python中 if __name__ == '__main__' 语句的解释
- 【python】if __name__ == '__main__'解释
- if __name__== "__main__" 的意思(作用)python代码复用
- python基础8之自定义模块、if __name__==__main__:解释
- 关于python中的if __name__ == '__main__':的解释
- Python中if __name__ == "__main__"详细解释
- python中if __name__ == "__main__":解释
- python中if __name__ == "__main__"的解释
- 关于python中__name__=='__main__'的解释
- if __name__== "__main__" 的意思(作用)python代码复用
- 转载过来的python if __name__ ='__main__'的解释
- python中if __name__ == "__main__"的详细解释及作用
- 一句话解释Python中 if _name_=='_main_'的作用
- python中if __name__ == "__main__"的解释
- 浅析python 中__name__ = '__main__' 的作用