_名称_变量及其在Python中的用法简介 (An introduction to the _ _name_ _ variable and its usage in Python)

You’ve most likely seen the __name__ variable when you’ve gone through Python code. Below you see an example code snippet of how it may look:

通过Python代码,您最有可能看到了__name__变量。 在下面,您可以查看其外观的示例代码片段:

if __name__ == '__main__':    main()

In this article, I want to show you how you can make use of this variable to create modules in Python.

在本文中,我想向您展示如何利用此变量在Python中创建模块。

为什么使用_名称_变量? (Why is the _ _name_ _ variable used?)

The __name__ variable (two underscores before and after) is a special Python variable. It gets its value depending on how we execute the containing script.

__name__变量(前后两个下划线)是一个特殊的Python变量。 它的取值取决于我们执行包含脚本的方式。

Sometimes you write a script with functions that might be useful in other scripts as well. In Python, you can import that script as a module in another script.

有时,您编写的脚本所包含的功能可能在其他脚本中也很有用。 在Python中,您可以将该脚本作为另一个脚本中的模块导入。

Thanks to this special variable, you can decide whether you want to run the script. Or that you want to import the functions defined in the script.

借助此特殊变量,您可以决定是否要运行脚本。 或者您要导入脚本中定义的功能。

__name__变量可以包含哪些值? (What values can the __name__ variable contain?)

When you run your script, the __name__ variable equals __main__. When you import the containing script, it will contain the name of the script.

运行脚本时, __name__变量等于__main__ 。 导入包含脚本时,它将包含脚本的名称。

Let us take a look at these two use cases and describe the process with two illustrations.

让我们看一下这两个用例,并用两个插图描述该过程。

方案1-运行脚本 (Scenario 1 - Run the script)

Suppose we wrote the script nameScript.py as follows:

假设我们编写了脚本 nameScript.py如下:

def myFunction():    print 'The value of __name__ is ' + __name__
def main():    myFunction()
if __name__ == '__main__':    main()

If you run nameScript.py, the process below is followed.

如果运行nameScript.py,则遵循以下过程。

Before all other code is run, the __name__ variable is set to __main__. After that, the main and myFunction def statements are run. Because the condition evaluates to true, the main function is called. This, in turn, calls myFunction. This prints out the value of __main__.

在运行所有其他代码之前,将__name__变量设置为__main__。 之后, mainmyFunction def语句运行。 因为条件的计算结果为true,所以将调用main函数。 依次调用myFunction。 这将打印出__main__的值。

方案2-将脚本导入另一个脚本 (Scenario 2 - Import the script in another script)

If we want to re-use myFunction in another script, for example importingScript.py, we can import nameScript.py as a module.

如果要在另一个脚本中重新使用myFunction,例如importingScript.py ,则可以将nameScript.py导入为模块。

The code in importingScript.py could be as follows:

importingScript.py的代码可能如下:

import nameScript as ns
ns.myFunction()

We then have two scopes: one of importingScript and the second scope of nameScript. In the illustration, you’ll see how it differs from the first use case.

然后,我们有两个作用域:一个是importingScript ,另一个是nameScript 。 在图中,您将看到它与第一个用例有何不同。

In importingScript.py the __name__ variable is set to __main__. By importing nameScript, Python starts looking for a file by adding .py to the module name. It then runs the code contained in the imported file.

在importingScript.py中, __name__变量设置为__main__。 通过导入nameScript,Python通过在模块名称中添加.py开始查找文件。 然后,它将运行导入文件中包含的代码。

But this time it is set to nameScript. Again the def statements for main and myFunction are run. But, now the condition evaluates to false and main is not called.

但是这次 设置为nameScript。 再次运行main和myFunction的def语句。 但是,现在条件评估为false且main不被调用。

In importingScript.py we call myFunction which outputs nameScript. NameScript is known to myFunction when that function was defined.

在importingScript.py中,我们调用myFunction来输出nameScript。 定义函数时,myFunction知道NameScript。

If you would print __name__ in the importingScript, this would output __main__. The reason for this is that Python uses the value known in the scope of importingScript.

如果要在importingScript中打印__name__ ,则将输出__main__ 。 原因是Python使用importingScript范围内的已知值。

结论 (Conclusion)

In this short article, I explained how you can use the __name__ variable to write modules. You can also run these modules on their own. This can be done by making use of how the values of these variables change depending on where they occur.

在这篇简短的文章中,我解释了如何使用__name__变量编写模块。 您也可以单独运行这些模块。 这可以通过利用这些变量的值根据它们发生的位置如何变化来完成。

翻译自: https://www.freecodecamp.org/news/whats-in-a-python-s-name-506262fe61e8/

(Python的)__ name__中包含什么?相关推荐

  1. python怎么判断字符串中包含特殊符号

    python判断字符串中包含特殊符号的方法:首先使用"for i in string"命令来遍历输入的字符串:然后输入"if i in input_psd"命令 ...

  2. Python检查Word文件中包含特定关键字的所有页码

    推荐教材:<Python程序设计基础与应用>(ISBN:9787111606178),董付国,机械工业出版社 图书详情: 配套资源: 用书教师可以联系董老师获取教学大纲.课件.源码.教案. ...

  3. python 字符串 双引号中包含双引号

    有点搞笑,因为这个点是看php代码学到的.很简单,为啥我自己想不到咧.. 先来看提供灵感的php代码: 比如sqli-labs的Less14,源代码中首先采用字符串拼接的方法给用户输入的参数两侧增加了 ...

  4. python字典编码_python中包含UTF-8编码中文的列表或字典的输出

    >>> dict = {"asdf": "我们的python学习"} >>> print dict {'asdf': '\x ...

  5. python语言包含的错误,Python语言程序中包含的错误,一般分为三种,以下____________不是其中的一种...

    Python语言程序中包含的错误,一般分为三种,以下____________不是其中的一种 答:编译错误 人体体温能自动调控在37度,其原因是( ). 答:人体内产生的热能是分批放出的 人体内有完善的 ...

  6. python列表中包含元祖_python列表与元祖

    python 的列表和元素: 共同点:有序的 区别:1.列表可以修改增加删除列表内容,元组不能修改 联系:元组中包含列表的元素,可以修改列表元素. 分析:1.列表:LIST1=[1,2,3,4,5,6 ...

  7. c字符串中包含双引号_码哥学Python,一起解密神秘的字符串密码

    哈喽,大家好,又到了晚上学习Python的时间了,想学习python的同学可以一起哦. 字符串 由0个或多个字符组成的有序字符序列,Python中的字符串用单引号 ' 或双引号 " 括起来, ...

  8. Python检查特定值是否包含在列表中

    python 检查特定值是否包含在列表中 # -*- coding: utf-8 -*-listA = ["23", "45", "78", ...

  9. python中第三方模块_如何在python脚本中包含第三方模块?

    我已经开始使用Python来自动化我工作中的重复任务,并且经常需要将对第三方模块的引用集成到我的脚本中.如何将这些文件直接包含在脚本中?我知道有一些方法可以在python安装文件夹(C:\Python ...

最新文章

  1. js学习总结----crm客户管理系统之项目开发流程和api接口文档
  2. 0821Cache Buffers chains与共享模式疑问4
  3. linux查看目录大小
  4. php 计划任务 curl,通过Task Scheduler定时运行调用cURL的PHP脚本 | 学步园
  5. Android Ormlite 学习笔记1 -- 基础
  6. 无法直接启动带有“类库输出类型”的项目
  7. js修改地址栏url_不同寻常的地址栏过渡
  8. AtCoder Grand Contest 021 D - Reversed LCS(区间dp)
  9. Oracle Class4. 数据库对象(同义词,序列,视图,索引,簇)
  10. 我说程序员要测试自己的代码,结果被怼!
  11. SQL Server 自动循环归档分区数据脚本
  12. firefox os 2.0版模拟器QQ初体验
  13. python查天气预报_一个用Python编写抓取天气预报的代码示例
  14. 84.LAMP的apache用户认证,域名跳转,日志文件
  15. 什么时候建立分区的时候需要建立EFI分区
  16. js-简易幻灯片制作
  17. python百钱百鸡问题_shell的循环与百鸡百钱问题
  18. springboot 2.0 配置全局时间格式化
  19. ACCESS数据库的压缩,备份,还原,下载,删除的实现
  20. Neuromation新研究:利用卷积神经网络进行儿童骨龄评估

热门文章

  1. HTML如何添加锚点,总结到位
  2. c语言 字符转int型,C语言—类型之间的转换
  3. 经典冒泡排序及其优化
  4. C++11并发编程:多线程std::thread
  5. 51Nod 1043 幸运号码
  6. eclipse类自动生成注释
  7. python使用GUI(图形用户界面)
  8. 74-A/D指标,Accumulation/Distribution,积累/派发线,离散指标.(2015.7.1)
  9. session 学习
  10. 什么原因成就了一位优秀的程序员?(转)