python学习系列(一)--- main函数
来源:互联网 发布:免费的自己的域名邮箱 编辑:程序博客网 时间:2024/05/17 09:18
关于Python的主(main)函数问题
初次接触Python的人会很不习惯Python没有main主函数。
这里简单的介绍一下,在Python中使用main函数的方法
#hello.py
def foo():
str="function"
print(str);
if __name__=="__main__":
print("main")
foo()
其中if __name__=="__main__":这个程序块类似与Java和C语言的中main(主)函数
在Cmd中运行结果
C:\work\python\divepy>python hello.py
main
function
在Python Shell中运行结果
>>> import hello
>>> hello.foo()
function
>>>hello.__name__
'hello'
>>>
可以发现这个内置属性__name__自动的发生了变化。
这是由于当你以单个文件运行时,__name__便是__main__
当你以模块导入使用时,这个属性便是这个模块的名字。
转载自http://blog.sina.com.cn/s/blog_71a7f0870100wndd.html
这里简单的介绍一下,在Python中使用main函数的方法
#hello.py
def foo():
if __name__=="__main__":
其中if __name__=="__main__":这个程序块类似与Java和C语言的中main(主)函数
在Cmd中运行结果
C:\work\python\divepy>python hello.py
main
function
在Python Shell中运行结果
>>> import hello
>>> hello.foo()
function
>>>hello.__name__
'hello'
>>>
可以发现这个内置属性__name__自动的发生了变化。
这是由于当你以单个文件运行时,__name__便是__main__
当你以模块导入使用时,这个属性便是这个模块的名字。
转载自http://blog.sina.com.cn/s/blog_71a7f0870100wndd.html
0 0
- python学习系列(一)--- main函数
- python学习(一)-函数
- Python学习基本系列笔记(一)
- Python学习笔记系列(一):异常
- Python学习系列(一)(基础入门)
- Python django 入门学习系列(一)
- Python学习系列(一)配置环境
- python函数学习一(跳出函数)
- pthread系列函数实例学习(一)
- 一---Android-SDK系列文章(6) ---JAVA 程序&& android应用程序(main函数)
- Python学习笔记(2) - Python的main函数
- Python学习系列:函数参数
- 【Python】主(main)函数的原理
- python main函数
- Python中的main函数
- python main 函数
- python main函数
- 【Python.基础】main函数
- 94:Binary Tree Inorder Traversal【树】【哈希】【栈】
- atitit.loading的设计与实现控件选型attilax 总结
- 献给这世上摇摆不定的人
- iOS 面试之三道题
- text下划线的实现方法
- python学习系列(一)--- main函数
- zlnfpxvljnv
- 海康RTSP格式
- Sass跟Scss 上手
- startActivityForResult用户及与onResume顺序问题
- 第五周项目6——类模板(友元函数)
- Python leveldb数据库 把txt的数据存入到leveldb
- Android 强制关闭虚拟键盘
- C语言字节对齐