python编程中的if __name__ == 'main': 的作用和原理
<p style="padding-top: 8px; padding-bottom: 8px; border: 0px; outline: 0px; font-size: medium; vertical-align: baseline; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; text-size-adjust: none; text-indent: 2em; color: rgb(0, 0, 0); font-family: Simsun;">在大多数编排得好一点的脚本或者程序里面都有这段if __name__ == 'main': ,虽然一直知道他的作用,但是一直比较模糊,收集资料详细理解之后与打架分享。</p><p style="padding-top: 8px; padding-bottom: 8px; border: 0px; outline: 0px; font-size: medium; vertical-align: baseline; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; text-size-adjust: none; text-indent: 2em; color: rgb(0, 0, 0); font-family: Simsun;"> 1、这段代码的功能</p><p style="padding-top: 8px; padding-bottom: 8px; border: 0px; outline: 0px; font-size: medium; vertical-align: baseline; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; text-size-adjust: none; text-indent: 2em; color: rgb(0, 0, 0); font-family: Simsun;"> 一个python的文件有两种使用的方法,第一是直接作为脚本执行,第二是import到其他的python脚本中被调用(模块重用)执行。因此if __name__ == 'main': 的作用就是控制这两种情况执行代码的过程,在if __name__ == 'main': 下的代码只有在第一种情况下(即文件作为脚本直接执行)才会被执行,而import到其他脚本中是不会被执行的。</p><p style="padding-top: 8px; padding-bottom: 8px; border: 0px; outline: 0px; font-size: medium; vertical-align: baseline; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; text-size-adjust: none; text-indent: 2em; color: rgb(0, 0, 0); font-family: Simsun;"> 举个例子,下面在test.py中写入如下代码:</p><div style="border: 0px; outline: 0px; font-size: 14px; vertical-align: baseline; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; text-size-adjust: none; color: rgb(17, 17, 17); font-family: "Microsoft YaHei", ���ź�, "Microsoft JhengHei", Verdana, Helvetica, sans-serif, ����;"><div style="border: 0px; outline: 0px; vertical-align: baseline; background: transparent; text-size-adjust: none;"><table style="border: 1px solid rgb(234, 237, 156); outline: 0px; vertical-align: baseline; background: transparent; border-spacing: 0px; width: 660px;"><tbody style="border: 0px; outline: 0px; vertical-align: baseline; background: transparent;"><tr style="border: 0px; outline: 0px; vertical-align: baseline; background: transparent;"><td style="padding: 0px; border-width: 0px; border-style: initial; border-color: initial; outline: 0px; vertical-align: baseline; background: transparent;"><div id="highlighter_310930" class="syntaxhighlighter" style="text-size-adjust: none; width: 651.406px; margin-top: 1em !important; margin-bottom: 1em !important; padding: 1px !important; border: 0px !important; outline: 0px !important; vertical-align: baseline !important; background-image: none !important; background-position: initial !important; background-size: initial !important; background-repeat: initial !important; background-attachment: initial !important; background-origin: initial !important; background-clip: initial !important; float: none !important; position: relative !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; line-height: 20px !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; min-height: auto !important;"><div class="lines" style="text-size-adjust: none; border: 0px !important; outline: 0px !important; vertical-align: baseline !important; background: none !important; float: none !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; line-height: 20px !important; min-height: auto !important;"><div class="line alt1" style="text-size-adjust: none; border: 0px !important; outline: 0px !important; vertical-align: baseline !important; background-image: none !important; background-position: initial !important; background-size: initial !important; background-repeat: initial !important; background-attachment: initial !important; background-origin: initial !important; background-clip: initial !important; float: none !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; line-height: 20px !important; min-height: auto !important;"><table style="border-spacing: 0px; border: 0px !important; outline: 0px !important; vertical-align: baseline !important; background: none !important; float: none !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; line-height: 20px !important; min-height: auto !important;"><tbody style="border: 0px !important; outline: 0px !important; vertical-align: baseline !important; background: none !important; float: none !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; line-height: 20px !important; min-height: auto !important;"><tr style="border: 0px !important; outline: 0px !important; vertical-align: baseline !important; background: none !important; float: none !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; line-height: 20px !important; min-height: auto !important;"><td class="number" style="padding: 0px !important; border-width: 0px !important; border-style: initial !important; border-color: initial !important; outline: 0px !important; vertical-align: top !important; background: none !important; float: none !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: 3em !important; line-height: 20px !important; min-height: auto !important; color: rgb(175, 175, 175) !important;"><code style="padding-right: 0.3em !important; border: 0px !important; outline: 0px !important; vertical-align: baseline !important; background: none !important; text-align: right !important; float: none !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: 2.7em !important; line-height: 20px !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; min-height: auto !important; display: block !important;">1</code></td><td class="content" style="overflow: hidden; padding: 0px 0px 0px 0.5em !important; border-width: 0px 0px 0px 3px !important; border-top-style: initial !important; border-right-style: initial !important; border-bottom-style: initial !important; border-top-color: initial !important; border-right-color: initial !important; border-bottom-color: initial !important; border-left-color: rgb(108, 226, 108) !important; outline: 0px !important; vertical-align: top !important; background: none !important; color: rgb(0, 0, 0) !important; line-height: 20px !important; width: auto !important; float: none !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; min-height: auto !important;"><code class="functions" style="border: 0px !important; outline: 0px !important; vertical-align: baseline !important; background: none !important; float: none !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; line-height: 20px !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; min-height: auto !important; color: rgb(255, 20, 147) !important;">print</code> <code class="string" style="border: 0px !important; outline: 0px !important; vertical-align: baseline !important; background: none !important; float: none !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; line-height: 20px !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; min-height: auto !important; color: blue !important;">"I'm the first."</code></td></tr></tbody></table></div><div class="line alt2" style="text-size-adjust: none; border: 0px !important; outline: 0px !important; vertical-align: baseline !important; background: none rgb(248, 248, 248) !important; float: none !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; line-height: 20px !important; min-height: auto !important;"><table style="border-spacing: 0px; border: 0px !important; outline: 0px !important; vertical-align: baseline !important; background: none !important; float: none !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; line-height: 20px !important; min-height: auto !important;"><tbody style="border: 0px !important; outline: 0px !important; vertical-align: baseline !important; background: none !important; float: none !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; line-height: 20px !important; min-height: auto !important;"><tr style="border: 0px !important; outline: 0px !important; vertical-align: baseline !important; background: none !important; float: none !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; line-height: 20px !important; min-height: auto !important;"><td class="number" style="padding: 0px !important; border-width: 0px !important; border-style: initial !important; border-color: initial !important; outline: 0px !important; vertical-align: top !important; background: none !important; float: none !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: 3em !important; line-height: 20px !important; min-height: auto !important; color: rgb(175, 175, 175) !important;"><code style="padding-right: 0.3em !important; border: 0px !important; outline: 0px !important; vertical-align: baseline !important; background: none !important; text-align: right !important; float: none !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: 2.7em !important; line-height: 20px !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; min-height: auto !important; display: block !important;">2</code></td><td class="content" style="overflow: hidden; padding: 0px 0px 0px 0.5em !important; border-width: 0px 0px 0px 3px !important; border-top-style: initial !important; border-right-style: initial !important; border-bottom-style: initial !important; border-top-color: initial !important; border-right-color: initial !important; border-bottom-color: initial !important; border-left-color: rgb(108, 226, 108) !important; outline: 0px !important; vertical-align: top !important; background: none !important; color: rgb(0, 0, 0) !important; line-height: 20px !important; width: auto !important; float: none !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; min-height: auto !important;"><code class="keyword" style="border: 0px !important; outline: 0px !important; vertical-align: baseline !important; background: none !important; float: none !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; line-height: 20px !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-weight: bold !important; min-height: auto !important; color: rgb(0, 102, 153) !important;">if</code> <code class="plain" style="border: 0px !important; outline: 0px !important; vertical-align: baseline !important; background: none !important; float: none !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; line-height: 20px !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; min-height: auto !important;">__name__</code><code class="keyword" style="border: 0px !important; outline: 0px !important; vertical-align: baseline !important; background: none !important; float: none !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; line-height: 20px !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-weight: bold !important; min-height: auto !important; color: rgb(0, 102, 153) !important;">=</code><code class="keyword" style="border: 0px !important; outline: 0px !important; vertical-align: baseline !important; background: none !important; float: none !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; line-height: 20px !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-weight: bold !important; min-height: auto !important; color: rgb(0, 102, 153) !important;">=</code><code class="string" style="border: 0px !important; outline: 0px !important; vertical-align: baseline !important; background: none !important; float: none !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; line-height: 20px !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; min-height: auto !important; color: blue !important;">"__main__"</code><code class="plain" style="border: 0px !important; outline: 0px !important; vertical-align: baseline !important; background: none !important; float: none !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; line-height: 20px !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; min-height: auto !important;">:</code></td></tr></tbody></table></div><div class="line alt1" style="text-size-adjust: none; border: 0px !important; outline: 0px !important; vertical-align: baseline !important; background-image: none !important; background-position: initial !important; background-size: initial !important; background-repeat: initial !important; background-attachment: initial !important; background-origin: initial !important; background-clip: initial !important; float: none !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; line-height: 20px !important; min-height: auto !important;"><table style="border-spacing: 0px; border: 0px !important; outline: 0px !important; vertical-align: baseline !important; background: none !important; float: none !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; line-height: 20px !important; min-height: auto !important;"><tbody style="border: 0px !important; outline: 0px !important; vertical-align: baseline !important; background: none !important; float: none !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; line-height: 20px !important; min-height: auto !important;"><tr style="border: 0px !important; outline: 0px !important; vertical-align: baseline !important; background: none !important; float: none !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; line-height: 20px !important; min-height: auto !important;"><td class="number" style="padding: 0px !important; border-width: 0px !important; border-style: initial !important; border-color: initial !important; outline: 0px !important; vertical-align: top !important; background: none !important; float: none !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: 3em !important; line-height: 20px !important; min-height: auto !important; color: rgb(175, 175, 175) !important;"><code style="padding-right: 0.3em !important; border: 0px !important; outline: 0px !important; vertical-align: baseline !important; background: none !important; text-align: right !important; float: none !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: 2.7em !important; line-height: 20px !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; min-height: auto !important; display: block !important;">3</code></td><td class="content" style="overflow: hidden; padding: 0px 0px 0px 0.5em !important; border-width: 0px 0px 0px 3px !important; border-top-style: initial !important; border-right-style: initial !important; border-bottom-style: initial !important; border-top-color: initial !important; border-right-color: initial !important; border-bottom-color: initial !important; border-left-color: rgb(108, 226, 108) !important; outline: 0px !important; vertical-align: top !important; background: none !important; color: rgb(0, 0, 0) !important; line-height: 20px !important; width: auto !important; float: none !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; min-height: auto !important;"><code class="spaces" style="border: 0px !important; outline: 0px !important; vertical-align: baseline !important; background: none !important; float: none !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; line-height: 20px !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; min-height: auto !important;"> </code><code class="functions" style="border: 0px !important; outline: 0px !important; vertical-align: baseline !important; background: none !important; float: none !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; line-height: 20px !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; min-height: auto !important; color: rgb(255, 20, 147) !important;">print</code> <code class="string" style="border: 0px !important; outline: 0px !important; vertical-align: baseline !important; background: none !important; float: none !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; line-height: 20px !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; min-height: auto !important; color: blue !important;">"I'm the second."</code></td></tr></tbody></table></div></div></div></td></tr></tbody></table></div></div><p style="padding-top: 8px; padding-bottom: 8px; border: 0px; outline: 0px; font-size: medium; vertical-align: baseline; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; text-size-adjust: none; text-indent: 2em; color: rgb(0, 0, 0); font-family: Simsun;">并直接执行test.py,结果如下图,可以成功print两行字符串。即,<span style="border: 0px; outline: 0px; font-size: 12px; vertical-align: baseline; background: transparent; color: rgb(255, 0, 0);">if __name__=="__main__": 语句之前和之后的代码都被执行</span>。</p><p style="padding-top: 8px; padding-bottom: 8px; border: 0px; outline: 0px; font-size: medium; vertical-align: baseline; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; text-size-adjust: none; text-indent: 2em; color: rgb(0, 0, 0); font-family: Simsun; text-align: center;"><em style="border: 0px; outline: 0px; font-size: 12px; vertical-align: baseline; background: transparent;"><img alt="python脚本测试" src="http://www.dengfeilong.com/uploads/allimg/170412/1054315501-0.png" style="border: 1px solid; outline: 0px; vertical-align: baseline; background: transparent; width: 400px; height: 142px;"></em></p><p style="padding-top: 8px; padding-bottom: 8px; border: 0px; outline: 0px; font-size: medium; vertical-align: baseline; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; text-size-adjust: none; text-indent: 2em; color: rgb(0, 0, 0); font-family: Simsun;"><br>然后在同一文件夹新建名称为import_test.py的脚本,只输入如代码:</p><div style="border: 0px; outline: 0px; font-size: 14px; vertical-align: baseline; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; text-size-adjust: none; color: rgb(17, 17, 17); font-family: "Microsoft YaHei", ���ź�, "Microsoft JhengHei", Verdana, Helvetica, sans-serif, ����;">import test<br>执行<span style="border: 0px; outline: 0px; font-size: medium; vertical-align: baseline; background: transparent; color: rgb(0, 0, 0); font-family: Simsun;">import_test.py脚本,输出结果如下:</span><br> </div><div style="border: 0px; outline: 0px; font-size: 14px; vertical-align: baseline; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; text-size-adjust: none; color: rgb(17, 17, 17); font-family: "Microsoft YaHei", ���ź�, "Microsoft JhengHei", Verdana, Helvetica, sans-serif, ����; text-align: center;"><span style="border: 0px; outline: 0px; font-size: medium; vertical-align: baseline; background: transparent; color: rgb(0, 0, 0); font-family: Simsun;"><img alt="if __name__=="__main__"演示" src="http://www.dengfeilong.com/uploads/allimg/170412/10543123H-1.png" style="border: 1px solid; outline: 0px; font-size: 12px; vertical-align: baseline; background: transparent; width: 400px; height: 61px;"></span></div><p style="padding-top: 8px; padding-bottom: 8px; border: 0px; outline: 0px; font-size: medium; vertical-align: baseline; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; text-size-adjust: none; text-indent: 2em; color: rgb(0, 0, 0); font-family: Simsun;">只输出了第一行字符串。即,<span style="border: 0px; outline: 0px; font-size: 12px; vertical-align: baseline; background: transparent; color: rgb(255, 0, 0);">if __name__=="__main__":</span> <span style="border: 0px; outline: 0px; font-size: 12px; vertical-align: baseline; background: transparent; color: rgb(255, 0, 0);">之前的语句被执行,之后的没有被执行</span>。<br><br> 2、运行的原理</p><p style="padding-top: 8px; padding-bottom: 8px; border: 0px; outline: 0px; font-size: medium; vertical-align: baseline; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; text-size-adjust: none; text-indent: 2em; color: rgb(0, 0, 0); font-family: Simsun;"> 每个python模块(python文件,也就是此处的test.py和import_test.py)都包含内置的变量__name__,当运行模块被执行的时候,__name__等于文件名(包含了后缀.py);如果import到其他模块中,则__name__等于模块名称(不包含后缀.py)。而“__main__”等于当前执行文件的名称(包含了后缀.py)。进而当模块被直接执行时,__name__ == 'main'结果为真。</p><p style="padding-top: 8px; padding-bottom: 8px; border: 0px; outline: 0px; font-size: medium; vertical-align: baseline; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; text-size-adjust: none; text-indent: 2em; color: rgb(0, 0, 0); font-family: Simsun;"> 同样举例说明,我们在test.py脚本的<span style="border: 0px; outline: 0px; font-size: 12px; vertical-align: baseline; background: transparent; color: rgb(255, 0, 0);">if __name__=="__main__":</span>之前加入print __name__,即将__name__打印出来。文件内容和结果如下,</p><p style="padding-top: 8px; padding-bottom: 8px; border: 0px; outline: 0px; font-size: medium; vertical-align: baseline; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; text-size-adjust: none; text-indent: 2em; color: rgb(0, 0, 0); font-family: Simsun; text-align: center;"><img alt="python脚本" src="http://www.dengfeilong.com/uploads/allimg/170412/105431O07-2.png" style="border: 1px solid; outline: 0px; font-size: 12px; vertical-align: baseline; background: transparent; width: 400px; height: 303px;"></p><p style="padding-top: 8px; padding-bottom: 8px; border: 0px; outline: 0px; font-size: medium; vertical-align: baseline; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; text-size-adjust: none; text-indent: 2em; color: rgb(0, 0, 0); font-family: Simsun;"> 可以看出,此时变量__name__的值为"__main__";<br> 再执行import_test.py,模块内容和执行结果如下:</p><p style="padding-top: 8px; padding-bottom: 8px; border: 0px; outline: 0px; font-size: medium; vertical-align: baseline; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; text-size-adjust: none; text-indent: 2em; color: rgb(0, 0, 0); font-family: Simsun; text-align: center;"> <img alt="__name__变量" src="http://www.dengfeilong.com/uploads/allimg/170412/1054311W9-3.png" style="border: 1px solid; outline: 0px; font-size: 12px; vertical-align: baseline; background: transparent; width: 400px; height: 161px;"></p><p style="padding-top: 8px; padding-bottom: 8px; border: 0px; outline: 0px; font-size: medium; vertical-align: baseline; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; text-size-adjust: none; text-indent: 2em; color: rgb(0, 0, 0); font-family: Simsun;"> 此时,test.py中的__name__变量值为test,不满足__name__=="__main__"的条件,因此,无法执行其后的代码。</p><p></p>
页:
[1]