Executing modules as scripts
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的更多相关文章
- Python Tutorial 学习(六)--Modules
6. Modules 当你退出Python的shell模式然后又重新进入的时候,之前定义的变量,函数等都会没有了. 因此, 推荐的做法是将这些东西写入文件,并在适当的时候调用获取他们. 这就是为人所知 ...
- [译]The Python Tutorial#6. Modules
[译]The Python Tutorial#Modules 6. Modules 如果你从Python解释器中退出然后重新进入,之前定义的名字(函数和变量)都丢失了.因此,如果你想写长一点的程序,使 ...
- Python模块学习
6. Modules If you quit from the Python interpreter and enter it again, the definitions you have made ...
- ValueError: Attempted relative import in non-package
执行:python deom/scripts/populate.py ValueError: Attempted relative import in non-package solve:python ...
- Python 中 -m 的典型用法、原理解析与发展演变
在命令行中使用 Python 时,它可以接收大约 20 个选项(option),语法格式如下: python [-bBdEhiIOqsSuvVWx?] [-c command | -m module- ...
- 关于 Python 的 import
好久以前就被 Python 的相对与绝对导入所困扰.去年粗浅探究后自以为完全理解,近来又因 sys.path[0] 和 os.getcwd() 的不一致而刷新了认知... Python 官方文档 5. ...
- debugging tools
https://blogs.msdn.microsoft.com/debugdiag/ https://blogs.msdn.microsoft.com/debuggingtoolbox/2012/1 ...
- 转://【MOS】关于在不同版本和平台之间进行还原或复制的常见问题 (文档 ID 1526162.1)--跨版本恢复
Questions and Answers 1) 我能用更高版本的 Oracle 还原或复制旧版本的数据库吗? 2) 我能在两个不同的补丁程序集之间进行还原或复制吗? 3) 我能在同一操作系统的不同版 ...
- 转:perl源码审计
转:http://www.cgisecurity.com/lib/sips.html Security Issues in Perl Scripts By Jordan Dimov (jdimov@c ...
随机推荐
- 关于JVM的类型和模式
原文出处: 摆渡者 引言 曾几何时,我也敲打过无数次这样的命令: 然而之前的我都只关心过版本号,也就是第一行的内容.今天,我们就来看看第3行输出的内容:JVM的类型和工作模式. 其实说Server和C ...
- 文件I/O
Linux顶层目录结构: / 根目录├── bin 存放用户二进制文件├── boot 存放内核引导配置文件├── dev 存放设备文件├── etc ...
- Android客户端向服务器端发送数据的流程(1)
原理: android客户端通过使用org.apache.http.impl.client.DefaultHttpClient类来发送数据; 方法介绍: HttpClient是android中提供的一 ...
- Web前端开发面试题
1. 以下的代码有问题吗?如果有你觉着应该如何修改? for(int i=0; i<list.size(); i++) { ..... ..... if(...) { list.re ...
- nyoj-----284坦克大战(带权值的图搜索)
坦克大战 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 Many of us had played the game "Battle city" ...
- OID,主键生成策略,PO VO DTO,get和load区别,脏检查,快照,java对象的三种状态
主键生成策略 sequence 数据库端 native 数据库端 uuid 程序端 自动赋值 生成的是一个32位的16进制数 实体类需把ID改成String 类型 assigned 程序端 需手 ...
- Asp.net 解析json
Asp.net Json数据解析的一种思路 http://www.cnblogs.com/scy251147/p/3317366.html http://tools.wx6.org/json2csha ...
- 【转】用PowerDesigner对现有的数据库反向工程建立E-R图
转自:http://www.cnblogs.com/oceanshare/archive/2010/02/10/1667071.html 由于早期的一个项目在数据库设计建立时没有输出为E-R图,偶在对 ...
- 采用SHELL,通过SQL LOAD导入一定格式的txt文件至数据库中
1. 准备工作,window中可直接通过sqlload直接导入文件,linux下,需要有sqlload的相关软件. 2. SQL脚本(MID_DFDZ.ctl) LOAD DATA INTO TABL ...
- ES mlockall作用——preventing that memory from being paged to the swap area
elasticsearch还有一个重要的参数bootstrap.mlockall,这个参数的目的是当你无法关闭系统的swap的时候,建议把这个参数设为true.防止在内存不够用的时候,elastics ...