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

# the following condition checks whether we are
# running as a script, in which case run the test code,
# or being imported, in which case don't. 英语渣没能准确得知作者要表达的意思,后来经过百度加上自己的摸索,终于搞明白了if __name__='__main__'的用法,

 if __name__='__main__'语句下面一般是测试语句,若是直接运行(Run)包含这个if语句的script文件,则会执行该测试语句;

如果是作为模块被导入至Python环境,则下面的测试语句不会被执行。即在save包含if语句的script文件后,比如存为Tutorial.py,那么你可以在Python的IDLE交互环境下直接输入import Tutorial,你会发现Tutorial模块导入成功了,但是并没有执行if __name__='__main__'语句下的测试语句。

PS:if __name__='__main__'中的下划线是两个"_",笔者在测试时候曾经以为只有一个下划线,而浪费大量时间找bug,最后还找不到(一脸懵逼),原谅我还没学会Debugger。

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__”: 有什么用

    https://stackoverflow.com/questions/419163/what-does-if-name-main-do# 问题: What does if name == " ...

  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++,以及完全面 ...

随机推荐

  1. j2ee之Filter使用实例(页面跳转)

    javax.servlet.Filter类中主要有三个方法. public void destroy(); //销毁对象 public void doFilter(ServletRequest req ...

  2. python学习3

    这部分学习python函数的写法: 在写python函数之前,首先看一下python中已经写好的函数,就像C中的<math>中的那些函数一样的东西有哪些:网址为https://docs.p ...

  3. transactionManager 以及datasource type解析

    transactionManager 在 MyBatis 中有两种事务管理器类型(也就是 type=”[JDBC|MANAGED]”): JDBC – 这个配置直接简单使用了 JDBC 的提交和回滚设 ...

  4. CentOS 6.5 升级 PHP 到5.6

    1. 首先先看下已安装的版本: yum list installed | grep php 可以看到我的都是5.3的 php.x86_64 -.el6_8 @updates php-cli.x86_6 ...

  5. Neural Pathways of Interaction Mediating the Central Control of Autonomic Bodily State 自主神经系统-大脑调节神经通路

    Figure above: Critchley H D, Harrison N A. Visceral influences on brain and behavior[J]. Neuron, 201 ...

  6. [LeetCode] Delete Node in a BST 删除二叉搜索树中的节点

    Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...

  7. [LeetCode] Reverse String 翻转字符串

    Write a function that takes a string as input and returns the string reversed. Example: Given s = &q ...

  8. [LeetCode] Range Sum Query 2D - Immutable 二维区域和检索 - 不可变

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  9. [LeetCode] Invert Binary Tree 翻转二叉树

    Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia: This problem wa ...

  10. [LeetCode] Combination Sum II 组合之和之二

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...