python中的 if __name__ == “__main__”: 有什么用
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__”: 有什么用的更多相关文章
- python中的if __name__ == '__main__' what hell is it?
python中的if __name__ == '__main__' what hell is it? python认为一切模块都可能被执行或者被import 如果一个模块是被import导入的,那么该 ...
- Python中的if __name__ == '__main__'
如何简单地理解Python中的if __name__ == '__main__' 1. 摘要 通俗的理解__name__ == '__main__':假如你叫小明.py,在朋友眼中,你是小明(__ ...
- python中使用if __name__ == '__main__':
引子 在python中,假设在一个test1.py的模块中定义了一个foo函数,然后调用函数foo进行测试的时候会产生一个内存空间.当你把这个模块导入到test2.py模块中,接下来如果在test2. ...
- Python中的if __name__='__main__'语句的作用
笔者在自学Python的过程中,对于if __name__='__main__'的用法感到很困惑,在think Python一书中原作者的源代码是这么解释if __name__='__main__'语 ...
- 扫盲贴000---关于python中的if __name__ == '__main__'
对于python中的__name__变量,根据调用python文件的方式不同,__name__变量的值也会不同.假如我有一个名为hello_world.py的python脚本,当我的目的是直接运行这个 ...
- python中的if __name__=='__main__': main()解析
python中我们会看到一段代码是这样的: if __name__=='__main__': main() 这段代码的什么意思,我们可以知道代码的意思是如果__name__=='__main__'为T ...
- 如何简单地理解Python中的if __name__ == '__main__'
https://blog.csdn.net/yjk13703623757/article/details/77918633 1. 摘要 通俗的理解__name__ == '__main__':假如你叫 ...
- 如何简单地理解Python中的if __name__ == '__main__'(https://blog.csdn.net/yjk13703623757/article/details/77918633)
1. 摘要 通俗的理解__name__ == '__main__':假如你叫小明.py,在朋友眼中,你是小明(__name__ == '小明'):在你自己眼中,你是你自己(__name__ == '_ ...
- 【转】Python 中的 if __name__ == '__main__' 该如何理解
转自:http://blog.konghy.cn/2017/04/24/python-entry-program/ 程序入口 对于很多编程语言来说,程序都必须要有一个入口,比如 C,C++,以及完全面 ...
- 如何快速简单粗暴地理解Python中的if __name__ == '__main__'
1. 摘要 通俗的理解__name__ == '__main__':假如你叫小明.py,在朋友眼中,你是小明(__name__ == '小明'):在你自己眼中,你是你自己(__name__ == '_ ...
随机推荐
- 江西理工大学南昌校区排名赛 C: 单身狗的地图游戏
题目描述 萌樱花是一只单身狗. 萌樱花今天决定去喜欢的学妹家玩,但他不记得路,于是他拿出有n个点的完全图,选取了k条不同的路. 完全图:n 个点的图中任意两个点之间都有一条边相连,所以有 n*(n-1 ...
- Codeforces - 570D 离散DFS序 特殊的子树统计 (暴力出奇迹)
题意:给定一棵树,树上每个节点有对应的字符,多次询问在\(u\)子树的深度为\(d\)的所有节点上的字符任意组合能否凑成一个回文串 把dfs序存储在一个二维线性表中,一个维度记录字符另一个维度记录深度 ...
- IE9以下程序开发不兼容项目罗列
1. 提前关闭iframe,脚本将不再执行 IE下的js不是缓存在站点系统中,而是缓存在每个单独页面系统中的,当关闭页面,对应引入的脚本将不再执行. 可能出现两种情况: iframe引入的html页面 ...
- Python+Selenium设置元素等待
显式等待 显式等待使 WebdDriver 等待某个条件成立时继续执行,否则在达到最大时长时抛弃超时异常 (TimeoutException). #coding=utf-8 from selenium ...
- 初探angular
前言 angular4.0目前已经发布了,angular是mvw框架,所以对其有一个简单的了解还是很有必要的. 目前angular有中文官网,且文档介绍也都是4.x的,但是为了了解其发展过程,我们先了 ...
- js动画实现&&回调地狱&&promise
1. js实现动画 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- vi编辑器备忘录
1. 基本操作 G 移动到文件最后一行 nG 移动到n行 gg 移动到第一行 N[Enter] 向下移动n行 /word 向下寻找 ?word 向上寻找 n 查找下一个 N 查找上一个 0或者Home ...
- 全文检索~solr的使用
全文检索这个系列在几前年写过lucene的文章,而现在看来它确实已经老了,它的儿子孙子都出来了,已经成为现在检索行列的主流,像solr,elasticsearch等,今天我们主要来看一个solr在as ...
- 用R处理不平衡的数据
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文来自云+社区翻译社,作者ArrayZoneYour 在分类问题当中,数据不平衡是指样本中某一类的样本数远大于其他的类别样本数.相比于多分 ...
- python 批量ping服务器
最近在https://pypi.python.org/pypi/mping/0.1.2找到了一个python包,可以用它来批量ping服务器,它是中国的大神写的,支持单个服务器.将服务器IP写在txt ...