首页 > 编程知识 正文

python的main函数是什么意思,python的main函数流程

时间:2023-05-05 20:49:50 阅读:184168 作者:249

代码样例 #hello.py# Set function foo;def foo(): str="function" print(str);# This is main function;if __name__=="__main__": print("main function.") foo()

其中if __name__=="__main__":表示的就是主函数的所在。

shell的运行结果 >>> import hello>>> hello.foo()function>>> hello.__name__'hello'>>>

shell的运行结果表明当前命令行所指变量对应的属性信息,而当该文件以模块格式导入时,该模块导入的属性为该文件的名字。

自动执行的代码

Python使用的是缩进对齐组织代码,即未缩进的部分将在载入时直接执行,因而不必有main函数。

import os,sysimport time print("Hello python !!!") def func_print():print("in func_print") def main():print("In main") #main()func_print()

运行结果:

$ ./main2.py Hello python !!!in func_print

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。