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




目录
相关文章
|
Python
python flask 后端报错 ImportError: cannot import name ‘cached_prope‘
问题python flask 后端报错 ImportError: cannot import name ‘cached_prope‘flask程序启动但抛出该错误,是因为werkzeug 版本过高,需要降低版本即可 解决:一般这种情况是需要注意第三方库版本的对应,werkzeug需要0.16.0 版本时 flask的版本应该时1.x.x 的版本,不能是2.x过高的版本。
190 0
|
5月前
|
Python
【Python】 已解决:NameError: name ‘python‘ is not defined
【Python】 已解决:NameError: name ‘python‘ is not defined
679 8
|
5月前
|
数据采集 机器学习/深度学习 Python
【Python】已完美解决:ImportError: cannot import name ‘Imputer‘ from ‘sklearn.preprocessing
【Python】已完美解决:ImportError: cannot import name ‘Imputer‘ from ‘sklearn.preprocessing
382 3
|
7月前
|
Python
python中if __name__ == '__main__'
python中if __name__ == '__main__'
51 3
|
4月前
|
API 开发工具 Python
【Azure Developer】Python 获取 Azure 中订阅(subscription)信息,包含ID, Name等
【Azure Developer】Python 获取 Azure 中订阅(subscription)信息,包含ID, Name等
|
6月前
|
Python
python中模块对象__name__
【6月更文挑战第12天】
118 7
|
7月前
|
Python
Python基础语法,解释一下Python中的if __name__ == "__main__"。
Python基础语法,解释一下Python中的if __name__ == "__main__"。
77 2
|
5月前
|
数据采集 前端开发 测试技术
【Python】已解决:(最新版selenium框架元素定位报错)NameError: name ‘By’ is not defined
【Python】已解决:(最新版selenium框架元素定位报错)NameError: name ‘By’ is not defined
137 0
|
5月前
|
Python
【Python】已解决:(pandas读取DataFrame列报错)raise KeyError(key) from err KeyError: (‘name‘, ‘age‘)
【Python】已解决:(pandas读取DataFrame列报错)raise KeyError(key) from err KeyError: (‘name‘, ‘age‘)
363 0
|
5月前
|
开发者 Python
【Python】已解决:NameError: name ‘reload’ is not defined
【Python】已解决:NameError: name ‘reload’ is not defined
253 0