__name__=='__main__':的用法

  1. python 文件的后缀为.py,比如 name.py

  2. python 文件可以直接执行,也可以被导入、调用,比如import name;

 

script 1:

1
2
3
4
5
6
#!/bin/python
# Filename:name.py
if  __name__== '__main__' :
     print  'This program is being run by itself'
else :
     print  'I am being imported from another module'

 

script 2:

1
2
3
#!/bin/python
# filename:test.py
import  name;
1
2
3
4
5
执行  test .py
返回
C:\Python27\python.exe C: /Users/Administrator/PycharmProjects/11/test .py
I am being imported from another module
Process finished with  exit  code 0

 

结论:

  1. __name__=='__main__' 代表直接执行

  2. 如果被当作模块导入到其他python文件中,执行else 后面的命令

  3. 一般我们在调试的时候会用到

转载于:https://www.cnblogs.com/tmdba/p/6381996.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值