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 pythoncat 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




目录
打赏
0
0
0
0
2
分享
相关文章
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过高的版本。
224 0
|
1月前
|
如何简单地理解Python中的if __name__ == '__main__'
本文介绍了Python中`__name__ == '__main__'`的作用和原理,解释了它如何作为程序入口控制代码执行。当.py文件直接运行时,`if __name__ == '__main__'`下的代码块会被执行;而当文件作为模块被导入时,该代码块不会执行。此外,文章还探讨了`__name__`变量在包结构中的作用,以及`__main__.py`文件与`python -m`命令的关系,详细说明了不同运行方式对模块路径的影响。
95 18
|
8月前
|
【Python】 已解决:NameError: name ‘python‘ is not defined
【Python】 已解决:NameError: name ‘python‘ is not defined
954 8
【Python】已完美解决:ImportError: cannot import name ‘Imputer‘ from ‘sklearn.preprocessing
【Python】已完美解决:ImportError: cannot import name ‘Imputer‘ from ‘sklearn.preprocessing
526 3
|
10月前
|
python中if __name__ == '__main__'
python中if __name__ == '__main__'
71 3
【Azure Developer】Python 获取 Azure 中订阅(subscription)信息,包含ID, Name等
【Azure Developer】Python 获取 Azure 中订阅(subscription)信息,包含ID, Name等
|
10月前
|
Python基础语法,解释一下Python中的if __name__ == "__main__"。
Python基础语法,解释一下Python中的if __name__ == "__main__"。
89 2
|
9月前
|
python中模块对象__name__
【6月更文挑战第12天】
144 7
【Python】已解决:(最新版selenium框架元素定位报错)NameError: name ‘By’ is not defined
【Python】已解决:(最新版selenium框架元素定位报错)NameError: name ‘By’ is not defined
240 0
|
8月前
|
【Python】已解决:(pandas读取DataFrame列报错)raise KeyError(key) from err KeyError: (‘name‘, ‘age‘)
【Python】已解决:(pandas读取DataFrame列报错)raise KeyError(key) from err KeyError: (‘name‘, ‘age‘)
701 0

热门文章

最新文章