这里简单的介绍一下,在Python中使用main函数的方法
#hello.py
def foo():
str="function"
print(str);
if __name__=="__main__":
print("main")
foo()
在Cmd中运行结果
C:\work\python\divepy>python hello.py
main
function
在Python Shell中运行结果
>>> import hello
>>> hello.foo()
function
>>> hello.__name__
'hello'
>>>
可以发现这个内置属性__name__自动的发生了变化。
这是由于当你以单个文件运行时,__name__便是__main__
当你以模块导入使用时,这个属性便是这个模块的名字。
6263



被折叠的 条评论
为什么被折叠?



