博客
关于我
_name_==‘’_main_‘’的解释
阅读量:396 次
发布时间:2019-03-05

本文共 675 字,大约阅读时间需要 2 分钟。

对于__name__ == "main"的理解

我为什么要写这个博客,是因为我刚开始也是因为不会而自己去总结了,嗯哼---------来吧,大白话来了

通俗 的理解就是在主脚本下运行,if__name__ =="__ main __" 上面下面的代码都会执行,当主脚本被import 到其他的文件下时,其之下的代码是不会被执行的。

举个例子:
创建一个excess.py文件,里面代码如下:

print("hello,word")  if __name__ == "__main__":  	print("你好!!!")

运行得到的结果就是 >>>hello,word

你好!!!
那么好,再创建一个excess1.py文件,代码:

import excess

运行第excess1文件,只打印了hello,word

#解释解释

为什么没有打印出“你好!!!”,因为在脚本中,if__name__ =="main"的解释,这里的__name__代表的是主脚本下的模块名称,当被导入到其他的脚本中后,__name__代表的可就不是主脚本的模块名称了,因为每个文件汇总都有内置变量__name __,所以说,当把excess导入到excess1文件后,在excess1下运行时主脚本下的if __name __ == “__main __” : 它的执行结果会是False,因此,if _ _ name _ _ == “__main __” :下的代码就不会被执行喽!!!
(__main__这东东的意思可以理解为当前文件名)

#哪里说的不好,请大神纠正

在这里插入图片描述

转载地址:http://nbxzz.baihongyu.com/

你可能感兴趣的文章
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named 'pandads'
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No static resource favicon.ico.
查看>>
no such file or directory AndroidManifest.xml
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>