python __name__使用.

简介: 在saltstack原码salt-2015.8.8.2/salt/version.py中 点击(此处)折叠或打开 if __name__ == '__main__':     print(_...
在saltstack原码salt-2015.8.8.2/salt/version.py中

点击(此处)折叠或打开

  1. if __name__ == '__main__':
  2.     print(__version__)
经常有程序这样写:

点击(此处)折叠或打开

  1. def main():
  2.     ......
  3.  
  4. if __name == "__main__":
  5.     main();


顺便学习了一下__name__


在模块中直接使用,__name__是__main__;
在模块中导入模块,__name__是模块名;
在类中使用,__name__是类名.

Modules…

Predefined (writable) attributes: __name__ is the module’s name;

… Classes…

Special attributes: __name__ is the class name;

29.4. __main__ — Top-level script environment

'__main__' is the name of the scope in which top-level code executes. A module’s __name__ is set equal to '__main__' when read from standard input, a script, or from an interactive prompt.

A module can discover whether or not it is running in the main scope by checking its own __name__, which allows a common idiom for conditionally executing code in a module when it is run as a script or with python -m but not when it is imported:

if __name__ == "__main__": # execute only if run as a script main() 

For a package, the same effect can be achieved by including a __main__.py module, the contents of which will be executed when the module is run with -m.




t@localhost python$ cat namemethod.py 
#!/usr/bin/env python3
def tprint():
    print('__name__ is %s' %(__name__))
if __name__ == '__main__':
    tprint()
else:
    print('import:')
    tprint()
t@localhost python$ ./namemethod.py 
__name__ is __main__
t@localhost python$ cat test.py 
#!/usr/bin/env python3
mport namemethod
tprint()
t@localhost python$ ./test.py 
import:
__name__ is namemethod
__name__ is namemethod




目录
相关文章
|
1月前
|
Python
python中if __name__ == '__main__'
python中if __name__ == '__main__'
17 3
|
4月前
|
Python
Python基础语法,解释一下Python中的if __name__ == "__main__"。
Python基础语法,解释一下Python中的if __name__ == "__main__"。
|
6月前
|
TensorFlow 算法框架/工具 Python
python报错:ImportError: cannot import name ‘_tf_stack‘ from ‘tenso
ImportError: cannot import name ‘_tf_stack’ from ‘tensorflow.python’本来keras和tensorflow用得好好的,忽然今天报错导入包得时候直接报错。在网上找了很多方法,但是用处都不大,尝试了很多遍都不行。于是尝试将tensorflow和keras卸载重装。(需要彻底卸载,pip命令后还需要删除文件夹中得几个文件夹,也就是...
62 1
|
3月前
|
索引 Python
完美解决丨#在python中,如果引用的变量未定义,则会报告NameError: name ‘变量名‘ is not defined。
完美解决丨#在python中,如果引用的变量未定义,则会报告NameError: name ‘变量名‘ is not defined。
|
4月前
|
前端开发 Python
Python 教程之变量(10)—— Python 中的 __name__ (一个特殊变量)
Python 教程之变量(10)—— Python 中的 __name__ (一个特殊变量)
33 0
|
8月前
|
存储 Python
python--导入,模块的引用,包,__name__
python--导入,模块的引用,包,__name__
|
9月前
|
算法 IDE 开发工具
【Python语法】类型提示(self, nums: List[int]) -> List[int],报错NameError: name ‘List‘ is not defined解决
【Python语法】类型提示(self, nums: List[int]) -> List[int],报错NameError: name ‘List‘ is not defined解决
|
10月前
|
测试技术 Python
Python|自动化测试与if __name__=="__main__":
Python|自动化测试与if __name__=="__main__":
71 0
|
11月前
|
机器学习/深度学习 人工智能 数据挖掘
|
12月前
|
Python
Python 的 __name__ 变量及其应用
__name__ 是 Python 中的一个特殊变量,它代表当前模块的名字。 当一个 Python 文件被直接运行的时候,__name__ 的值会被设置为 __main__。 当代码被导入到其他模块中运行时,__name__ 的值会被设置为模块名称。