[python刷题]pyhton中的main()方法是什么意思

描述

if __name__ == "__main__":可以看成是python程序的入口,就像java中的main()方法,但不完全正确。
事实上python程序是从上而下逐行运行的,在.py文件中,除了def后定义函数外的代码都会被认为是“main”方法中的内容从上而下执行。

例子


def sayHello():
    str = "hello"
    print(str);
    print(__name__+'from hello.sayhello()')
 
 
if __name__ == "__main__":
    print ('This is main of module "hello.py"')
    sayHello()
    print(__name__+'from hello.main')

运行结果:

This is main of module "hello.py"
__main__from hello.main

猜你喜欢

转载自blog.csdn.net/TOMOCAT/article/details/86062808
今日推荐