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. 移动距离--dfs-蓝桥杯

    题目描述: X星球居民小区的楼房全是一样的,并且按矩阵样式排列.其楼房的编号为1,2,3... 当排满一行时,从下一行相邻的楼往反方向排号. 比如:当小区排号宽度为6时,开始情形如下:   1   2 ...

  2. 爬虫beautifulsoup实践

    爬虫beautifulsoup实践: 目的:在https://unsplash.com/上爬取图片并保存到本地文件夹里.   一.观察response.首先,在Chrome浏览器里观察一下该网页的re ...

  3. Docker:网络模式详解

    Docker作为目前最火的轻量级容器技术,牛逼的功能,如Docker的镜像管理,不足的地方网络方面. Docker自身的4种网络工作方式,和一些自定义网络模式 安装Docker时,它会自动创建三个网络 ...

  4. oracle 基础知识(七)----动态性能视图

    一,动态性能视图介绍 动态性能视图属于数据字典,系统管理员用户 SYS 可以访问它们.在数据库打开和使用时不断进行更新,而且它们的内容主要与性能有关.虽然这些视图很像普通的数据库表,但它们不允许用户直 ...

  5. 【3dsMax安装失败,如何卸载、安装3dMax 2013?】

    AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...

  6. TOJ 4003 Next Permutation

    描述 It is an interesting exercise to write a program to print out all permutations of 1, 2, …, n. How ...

  7. Eclipse设置JVM的内存参数

    打开Eclipse 或者 MyEclipse 打开 Windows -> Preferences -> Java -> Installed JREs 选中你所使用的 JDK,然后点击 ...

  8. 通过TLS回调函数的反调试

    下面是TLS数据结构的定义 typedef struct _IMAGE_TLS_DIRECTORY { DWORD StartAddressOfRawData; DWORD EndAddressOfR ...

  9. 深入理解JavaScript系列(25):设计模式之单例模式

    介绍 从本章开始,我们会逐步介绍在JavaScript里使用的各种设计模式实现,在这里我不会过多地介绍模式本身的理论,而只会关注实现.OK,正式开始. 在传统开发工程师眼里,单例就是保证一个类只有一个 ...

  10. [转].NET Core之Entity Framework Core 你如何创建 DbContext

    本文转自:http://www.cnblogs.com/tdws/p/5874212.html 本文版权归博客园和作者吴双共同所有,欢迎转载,转载和爬虫请注明博客园蜗牛原文地址 http://www. ...