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).

Executing modules as scripts的更多相关文章

  1. Python Tutorial 学习(六)--Modules

    6. Modules 当你退出Python的shell模式然后又重新进入的时候,之前定义的变量,函数等都会没有了. 因此, 推荐的做法是将这些东西写入文件,并在适当的时候调用获取他们. 这就是为人所知 ...

  2. [译]The Python Tutorial#6. Modules

    [译]The Python Tutorial#Modules 6. Modules 如果你从Python解释器中退出然后重新进入,之前定义的名字(函数和变量)都丢失了.因此,如果你想写长一点的程序,使 ...

  3. Python模块学习

    6. Modules If you quit from the Python interpreter and enter it again, the definitions you have made ...

  4. ValueError: Attempted relative import in non-package

    执行:python deom/scripts/populate.py ValueError: Attempted relative import in non-package solve:python ...

  5. Python 中 -m 的典型用法、原理解析与发展演变

    在命令行中使用 Python 时,它可以接收大约 20 个选项(option),语法格式如下: python [-bBdEhiIOqsSuvVWx?] [-c command | -m module- ...

  6. 关于 Python 的 import

    好久以前就被 Python 的相对与绝对导入所困扰.去年粗浅探究后自以为完全理解,近来又因 sys.path[0] 和 os.getcwd() 的不一致而刷新了认知... Python 官方文档 5. ...

  7. debugging tools

    https://blogs.msdn.microsoft.com/debugdiag/ https://blogs.msdn.microsoft.com/debuggingtoolbox/2012/1 ...

  8. 转://【MOS】关于在不同版本和平台之间进行还原或复制的常见问题 (文档 ID 1526162.1)--跨版本恢复

    Questions and Answers 1) 我能用更高版本的 Oracle 还原或复制旧版本的数据库吗? 2) 我能在两个不同的补丁程序集之间进行还原或复制吗? 3) 我能在同一操作系统的不同版 ...

  9. 转:perl源码审计

    转:http://www.cgisecurity.com/lib/sips.html Security Issues in Perl Scripts By Jordan Dimov (jdimov@c ...

随机推荐

  1. python中字符串与列表的相互转换

    列表转字符串 list1 = ['abc' , 'def' , 'ghi'] str1 = ','.join(list1) str1 = '##'.join(list1) 字符串转列表 str1 = ...

  2. VI编辑器学习笔记

    VIM的使用 VI是Linux系统中的一种编辑器,它的使用方法和界面与Unix平台十分相似,掌握了VIM的特殊,你可以感觉到它强大的功能与高效.Vim 相对来说较小,无论你使用任何Linux系统,你总 ...

  3. 原生JavaScript实现mouseenter

    mouseenter和Mouseleave都是jquery的事件,JavaScript的mouseover和mouseout每个子元素都会触发,从子元素移到父元素也会触发,用起来不很方便,而且触发的太 ...

  4. Python输出内容的三种方式:print输出 python脚本执行 linux直接执行

    1.  在linux中安装python后,在linux命令行中输入python即可切换到Python命令行下 退出python命令行的命令: 老版本:ctrl+D 新版本:quit();或exit() ...

  5. oracle用户

    如果要了解oracle中用户信息,可以查询数据字典dba_users.在sql*plus中,使用system用户登录,查询语句如下: select username,account_status fr ...

  6. VBA读取文件夹下所有文件夹及文件内容,并以树形结构展示

    Const TR_LEVEL_MARK = "+"Const TR_COL_INDEX = "A"Const TR_COL_LEVEL = "E&qu ...

  7. @property和@synthesize的特性

    基础回顾:get方法和set方法 定义类成员变量时,可以在@interface中定义,也可以在@implementation中定义: 在@interface中声明,成员变量的状态是受保护的,即“@pr ...

  8. 可伸缩的textview。

    在一些应用中,比如腾讯的应用市场APP应用宝,关于某款应用的介绍文字,如果介绍文字过长,那么不是全部展现出来,而是显示三四行的开始部分(摘要),预知全部的内容,用户点击展开按钮即可查阅全部内容. 这样 ...

  9. function gzdecode

    function gzdecode($data) { return gzinflate(substr($data,10,-8)); } 因为项目要下载一个gzip压缩的网页,所以需要解压,手册上有一个 ...

  10. PowerMock遇到的问题——3

    在用WhiteBox调用对象的私有方法时,如果要传的参数为空,如果直接在参数列表中写null的话,可能会报空指针异常,可以定义一个变量使他等于空,再把变量传进去就可以了.