https://stackoverflow.com/questions/419163/what-does-if-name-main-do#

问题:

What does if name == “main”: do?

# Threading example
import time, thread def myfunction(string, sleeptime, lock, *args):
while True:
lock.acquire()
time.sleep(sleeptime)
lock.release()
time.sleep(sleeptime) if __name__ == "__main__":
lock = thread.allocate_lock()
thread.start_new_thread(myfunction, ("Thread #: 1", 2, lock))
thread.start_new_thread(myfunction, ("Thread #: 2", 2, lock))

解答:

当 Python 解释器读取源文件时, 它将执行在其中找到的所有代码。

在执行代码之前, 它将定义一些特殊的变量。例如, 如果 Python 解释器正在将该模块 (源文件) 作为主程序运行, 则会将特殊的 "name" 变量设置为具有值 "main "。如果从另一个模块导入此文件, "name" 将被设置为模块的名称。

在您的脚本的情况下, 让我们假设它的执行作为主要功能, 例如, 你说的东西像

python threading_example

在命令行上。设置特殊变量后, 它将执行导入语句并加载这些模块。然后, 它将评估 def 块, 创建一个函数对象, 并创建一个名为 myfunction 的变量, 指向函数对象。然后, 它将读取 if 语句, 并看到 name 执行相等 "main ", 因此它将运行显示在那里的块。

这样做的一个原因是, 有时您编写的模块 (. py 文件) 可以直接执行。或者, 它也可以导入并在另一个模块中使用。通过执行main检查, 只有当您希望将模块作为程序运行时才执行该代码. 而当有人只想导入您的模块并调用您的函数时, 不执行main函数。

原文:

When the Python interpreter reads a source file, it executes all of the code found in it.

Before executing the code, it will define a few special variables. For example, if the Python interpreter is running that module (the source file) as the main program, it sets the special __name__ variable to have a value "main". If this file is being imported from another module, __name__ will be set to the module's name.

In the case of your script, let's assume that it's executing as the main function, e.g. you said something like

python threading_example.py

on the command line. After setting up the special variables, it will execute the import statement and load those modules. It will then evaluate the def block, creating a function object and creating a variable called myfunction that points to the function object. It will then read the if statement and see that name does equal "main", so it will execute the block shown there.

One reason for doing this is that sometimes you write a module (a .py file) where it can be executed directly. Alternatively, it can also be imported and used in another module. By doing the main check, you can have that code only execute when you want to run the module as a program and not have it execute when someone just wants to import your module and call your functions themselves.

python中的 if __name__ == “__main__”: 有什么用的更多相关文章

  1. python中的if __name__ == '__main__' what hell is it?

    python中的if __name__ == '__main__' what hell is it? python认为一切模块都可能被执行或者被import 如果一个模块是被import导入的,那么该 ...

  2. Python中的if __name__ == '__main__'

    如何简单地理解Python中的if __name__ == '__main__'   1. 摘要 通俗的理解__name__ == '__main__':假如你叫小明.py,在朋友眼中,你是小明(__ ...

  3. python中使用if __name__ == '__main__':

    引子 在python中,假设在一个test1.py的模块中定义了一个foo函数,然后调用函数foo进行测试的时候会产生一个内存空间.当你把这个模块导入到test2.py模块中,接下来如果在test2. ...

  4. Python中的if __name__='__main__'语句的作用

    笔者在自学Python的过程中,对于if __name__='__main__'的用法感到很困惑,在think Python一书中原作者的源代码是这么解释if __name__='__main__'语 ...

  5. 扫盲贴000---关于python中的if __name__ == '__main__'

    对于python中的__name__变量,根据调用python文件的方式不同,__name__变量的值也会不同.假如我有一个名为hello_world.py的python脚本,当我的目的是直接运行这个 ...

  6. python中的if __name__=='__main__': main()解析

    python中我们会看到一段代码是这样的: if __name__=='__main__': main() 这段代码的什么意思,我们可以知道代码的意思是如果__name__=='__main__'为T ...

  7. 如何简单地理解Python中的if __name__ == '__main__'

    https://blog.csdn.net/yjk13703623757/article/details/77918633 1. 摘要 通俗的理解__name__ == '__main__':假如你叫 ...

  8. 如何简单地理解Python中的if __name__ == '__main__'(https://blog.csdn.net/yjk13703623757/article/details/77918633)

    1. 摘要 通俗的理解__name__ == '__main__':假如你叫小明.py,在朋友眼中,你是小明(__name__ == '小明'):在你自己眼中,你是你自己(__name__ == '_ ...

  9. 【转】Python 中的 if __name__ == '__main__' 该如何理解

    转自:http://blog.konghy.cn/2017/04/24/python-entry-program/ 程序入口 对于很多编程语言来说,程序都必须要有一个入口,比如 C,C++,以及完全面 ...

  10. 如何快速简单粗暴地理解Python中的if __name__ == '__main__'

    1. 摘要 通俗的理解__name__ == '__main__':假如你叫小明.py,在朋友眼中,你是小明(__name__ == '小明'):在你自己眼中,你是你自己(__name__ == '_ ...

随机推荐

  1. aoj0558

    一.题意: 在H * W的地图上有N个奶酪工厂,分别生产硬度为1-N的奶酪.有一只吃货老鼠准备从老鼠洞出发吃遍每一个工厂的奶酪.老鼠有一个体力值,初始时为1,每吃一个工厂的奶酪体力值增加1(每个工厂只 ...

  2. Linux混杂设备驱动

    1. Linux混杂设备驱动模型 ① 在Linux系统中,存在一类字符设备,它们拥有相同的主设备号(10),但次设备号不同,我们称这类设备为混杂设备(miscdevice).所有混杂设备形成一个链表, ...

  3. js 递归思想 处理后台多维数组的数据 之 完美契合

    不多BB! 直接看源码 get(tree = []) { let self = this let arr = []; if (!!tree && tree.length !== 0) ...

  4. python数组(列表、元组及字典)

    python数组的使用 2010-07-28 17:17 1.Python的数组分三种类型: (1) list 普通的链表,初始化后可以通过特定方法动态增加元素. 定义方式:arr = [元素] (2 ...

  5. (转)[Nginx] – 配置文件优化 [一 ,二]

    [Nginx] – 安全优化 – 配置文件优化 [二] 原文:https://www.abcdocker.com/abcdocker/586 [Nginx] – 性能优化 – 配置文件优化 [一] 原 ...

  6. 读《NoSQL精粹》前三章有感

    现在NoSQL很流行,所以买了一本这方面的书,这本书虽然很薄 156页,但是感觉的确是大师的经验之谈,对于自己经验还是很少.无法能完全能心领神会,大师所说的,就像一个人说药苦,你没吃过.再听别人描述也 ...

  7. JAVA 利用反射自定义数据层框架

    之前的随笔一直都在介绍c#,主要公司最近的业务都是做桌面程序,那么目前c#中的WPF肯定是我做桌面程序的不二之选,做了半年的WPF,也基本摸清了c#写代码的套路和规则(本人之前是两年多的JAVA开发者 ...

  8. Java学习第二十二天

    1:登录注册IO版本案例(掌握) 要求,对着写一遍. cn.itcast.pojo User cn.itcast.dao UserDao cn.itcast.dao.impl UserDaoImpl( ...

  9. VS 连接数据库报错:在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误

    VS报错:在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.未找到或无法访问服务器.请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接. (provider ...

  10. 【学习笔记】Java实用类:枚举(Enum)

    Java API(Java Application Programming Interface)Java应用程序编程接口... 实用类:Java API提供了几个常用包: 1.java.lang:编写 ...