您的位置:首页 > 编程语言 > Python开发

Python,如果__name__ == __main__用代码示例解释

2020-08-21 08:35 2691 查看

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

.py
file 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.py
and 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.py
and paste this code inside:

现在添加另一个名为

file_two.py
文件,并将此代码粘贴到其中:

Also, modify the code in

file_one.py
like this so we import the
file_two
module:

另外,像这样修改

file_one.py
的代码,以便我们导入
file_two
模块:

Running our

file_one
code once again will show that the
__name__
variable in the
file_one
did not change, and still remains set to
__main__
. But now the variable
__name__
in
file_two
is 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_two
directly 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_one
and
file_two
to look like this:

修改

file_one
file_two
如下所示:

file_one
:

file_one

file_two
:

file_two

Again, when running

file_one
you will see that the program recognized which of these two modules is
__main__
and executed the code according to our first
if else
statements.

同样,在运行

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_two
and 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_one
to 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_one
you 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_one
to 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_two
module is really big with lot of functions (two in our case), and you don't want to import all of them. Modify
file_two
to look like this:

现在,假设

file_two
模块确实具有很多功能(在我们的示例中为两个)非常大,并且您不想导入所有这些功能。 修改
file_two
如下所示:

And to import the specific functions from the module, use the

from
import block in the
file_one
file:

要从模块导入特定功能,请使用

file_one
文件中的
from
import块:

结论 (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/

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: