打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
python __name__ 变量

摘抄自:廖雪峰老师博客

编写一个hello模块:

#!/usr/bin/env python3# -*- coding: utf-8 -*-' a test module '__author__ = 'Michael Liao'import sysdef test():    args = sys.argv    if len(args)==1:        print('Hello, world!')    elif len(args)==2:        print('Hello, %s!' % args[1])    else:        print('Too many arguments!')if __name__=='__main__':    test()

当我们在命令行运行hello模块文件时,Python解释器把一个特殊变量__name__置为__main__,而如果在其他地方导入该hello模块时,if判断将失败,因此,这种if测试可以让一个模块通过命令行运行时执行一些额外的代码,最常见的就是运行测试。


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
使用模块--python
函数1_Python
Python 基础(十七):装饰器
python中__name__的使用
Python中import机制详解
python3
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服