When you run a Python module with

python fibo.py <arguments>

the code in the module will be executed, just as if you imported it, but with the __name__ set to "__main__". That means that by adding this code at the end of your module:

if __name__ == "__main__":
import sys
fib(int(sys.argv[1]))

you can make the file usable as a script as well as an importable module, because the code that parses the command line only runs if the module is executed as the “main” file:

$ python fibo.py 50
1 1 2 3 5 8 13 21 34

If the module is imported, the code is not run:

>>>

>>> import fibo
>>>

This is often used either to provide a convenient user interface to a module, or for testing purposes (running the module as a script executes a test suite).

最新文章

  1. Service 广播 到Fragment
  2. groupspecWidhoutAuthorizations与groupspecWidthAuthorizations的区别
  3. MFC ADO连接Sql Server数据库报无效指针的问题
  4. [转] 利用SET STATISTICS IO和SET STATISTICS TIME 优化SQL Server查询性能
  5. java抓取快递信息
  6. oracle的用户
  7. 在Iframe框架下如何跳转到登录界面
  8. C#调用WebService实例和开发
  9. Win7 Cygwin环境试验Nutch tutorial遇到的异常解决方法
  10. (11.28)Java小知识!
  11. Nginx作为HTTP服务器--Nginx配置图片服务器
  12. linux入门--Linux和UNIX的关系及区别
  13. 主引导扇区MBR的解析
  14. [ZJOI2012]旅游
  15. (转载)windows下安装配置Xampp
  16. RecyclerView拖拽排序;
  17. adb push和adb install区别
  18. Revit API单位转换类
  19. Postgres快速创建大量测试数据
  20. phpstorm中FTP自动同步功能

热门文章

  1. iframe-摘自网友
  2. (巨坑)改了tpl文件之后,前端效果没反应
  3. K2工作流的使用
  4. BZOJ1230 [Usaco2008 Nov]lites 开关灯
  5. [转载]Windows 7笔记本创建wifi热点供手机上网教程
  6. CentOS查看软件源提供的软件版本命令
  7. [Hadoop入门] - 1 Ubuntu系统 Hadoop介绍 MapReduce编程思想
  8. 类似UC天气下拉和微信下拉眼睛头部弹入淡出UI交互效果(开源项目)。
  9. bzoj 1934: [Shoi2007]Vote 善意的投票
  10. hdu 4607 Park Visit