[英]What is the purpose of __name__?
__name__
做什么的? 我只看到它与__main__
配对,仅此而已。
我知道经典的if __name__ == __main__:
定义了作为包与独立运行时的行为。
但是__name__
还有什么其他用法?
如果您直接执行脚本,则__name__
是"__main__"
。 如果要导入模块,则__name__
是模块的名称。
foo.py:
print(__name__)
bar.py
import foo
运行脚本:
$ python foo.py
__main__
$ python bar.py
foo
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.