__name__ 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()
如果希望代码仅在直接执行时触发,请使用
if __name__ == '__main__'
条件:此代码通常称为
global scope code
,module level code
或top-level code
但是它没有合适的命名约定,但是当您使用其中任何一个时,程序员会理解您的意思。你知道吗
相关问题 更多 >
编程相关推荐