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 .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_onefile_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/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值