Python之first script
1 A first script
1) script1.py
- imports a Python module (libraries of additional tools) to fetch the name of the platform
- calls three print function to display the script’s results
- uses a variable named x, created when it’s assigned, to hold onto a string object
# A first Python script
import sys # Load a library module
print(sys.platform)
print(2 ** 10) # Raise 2 to a power
x = 'Spam!'
print(x * 8) # String repetition
2) Run
$ python script1.py
linux2
1024
Spam!Spam!Spam!Spam!Spam!Spam!Spam!Spam!
2 Module Imports and Reloads
1) Module
Modules are simply text files containing Python statements. Python executes all the code in a module file from top to bottom each time we run the file. Each file is a module, and modules import other modules to use the names they define. Modules are processed with two statements and one important function:
import - Lets a client (importer) fetch a module as a whole
from - Allows clients to fetch particular names from a module
imp.reload - Provides a way to reload a module’s code without stopping Python

2) Import
Every file of Python source code whose name ends in a .py extension is a module. Other files can access the items a module defines by importing that module; import operations essentially load another file and grant access to that file’s contents.
$ python
Python 2.7.3 (default, Dec 18 2014, 19:10:20)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import script1
linux2
1024
Spam!Spam!Spam!Spam!Spam!Spam!Spam!Spam!
This works, but only once per session (really, process) by default. After the first import, later imports do nothing, even if you change and save the module’s source file again in another window.
This is by design; imports are too expensive an operation to repeat more than once per file, per program run. Imports must find files, compile them to byte code, and run the code.
3) Reload
If want to force Python to run the file again in the same session without stopping and restarting the session, we need to instead call the reload function available in the imp standard library module (this function is also a simple built-in in Python 2.6, but not in 3.0).
>>> from imp import reload
>>> reload(script1)
linux2
1024
Spam!Spam!Spam!Spam!Spam!Spam!Spam!Spam!
<module 'script1' from 'script1.pyc'>
Python之first script的更多相关文章
- python BeautifulSoup4 获取 script 节点问题
在爬取12306站点名时发现,BeautifulSoup检索不到station_version的节点 因为script标签在</html>之外,如果用‘lxml’解析器会忽略这一部分,而使 ...
- python click module for command line interface
Click Module(一) ----xiaojikuaipao The following mat ...
- Python框架、库以及软件资源汇总
转自:http://developer.51cto.com/art/201507/483510.htm 很多来自世界各地的程序员不求回报的写代码为别人造轮子.贡献代码.开发框架.开放源代码使得分散在世 ...
- python工具的安装
下载: python安装包:python-2.7.3.msi pywin32-218.win32-py2.7.exe setuptools安装包:setuptools-0.6c11.win32-py2 ...
- Embeding Python & Extending Python with FFPython
Introduction ffpython is a C++ lib, which is to simplify tasks that embed Python and extend Python. ...
- Machine and Deep Learning with Python
Machine and Deep Learning with Python Education Tutorials and courses Supervised learning superstiti ...
- Python在Windows下开发环境配置汇总
最近比较关注学习Python方面的资料和课程,由于Python本身基本都是在Linux下开发,本人windows用习惯了初用Linux各种别扭啊. 下面将我在配置Windows环境下的禁言写出来,与大 ...
- python 操作mongodb数据库模糊查询
# -*- coding: utf-8 -*-import pymongoimport refrom pymongo import MongoClient #创建连接#10.20.66.106clie ...
- [Medusa-dev] psp_handler - embed python in HTML like ASP
[Medusa-dev] psp_handler - embed python in HTML like ASP [Medusa-dev] psp_handler - embed python in ...
随机推荐
- MapReduce Shuffle优化方向
Shuffle过程介绍可以查看该博客:http://langyu.iteye.com/blog/992916 优化方向: 压缩:对数据进行压缩,减少写读数据量: 减少不必要的排序:并不是所有类型的Re ...
- 1414 冰雕 51nod 暴力
1414 冰雕 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 关注 白兰大学正在准备庆祝成立256周年.特别任命副校长 ...
- 洛谷 P1491 集合位置
P1491 集合位置 题目描述 每次有大的活动,大家都要在一起“聚一聚”,不管是去好乐迪,还是避风塘,或者汤姆熊,大家都要玩的痛快.还记得心语和花儿在跳舞机上的激情与释放,还记得草草的投篮技艺是如此的 ...
- 相克军_Oracle体系_随堂笔记 PPT
http://www.cnblogs.com/jyzhao/category/581259.html http://download.csdn.net/detail/yzj149286454/8960 ...
- 利用DTrace实时检测MySQl
与我们大多数人想象的不同,DTrace用于MySQL时不需对MySQL做任何更改.DTrace最强大的“提供器”(provider,是一组可观测的探测器)是FBT(Functional Boundar ...
- [Python]Django部署问题
给客户部署系统 Apache + mod_wsgi + django 打开浏览器妈的死活就是pending状态 运来mod_wsgi用的32位,操作系统64位(window server 2003 r ...
- cppunit 单元测试程序
原文:http://www.cnblogs.com/wang-kai/p/5106740.html 极限编程(XP)推崇测试优先原则,由此引发了软件开发方法从传统的瀑布模型转向以测试为驱动的敏捷开发模 ...
- 很强大的shell写的俄罗斯方块
网上看到的一个用linux的shell脚本写的俄罗斯方块. 是我至今见过写的最牛逼的shell了.共享一下. 原作者信息在脚本的凝视中有. 下载地址:点击下载 #!/bin/bash # Tetris ...
- 创造HTTPS的是个神
HTTP 是一个明文传输的协议,很多网络监听工具都可以轻易窃取网络中传输的用户信息,如密码,信用卡, 直到后来发明HTTPS, 世界一下子安静了 Why HTTPS? HTTPS可以保证用户提交的信息 ...
- jQuery总结02
1 如何搭建一个 jQuery 环境? 2 jQuery 对象与 DOM 对象一样吗?区别是什么? 3 jQuery选择器类型有哪些?