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 ...
随机推荐
- 转:Windows Phone 7 设计简介
英文原文:smashingmagazine 导读:Windows Phone 7 引进了一个全新的内容管理和用户界面,命名为Metro的设计语言和理论.微软这次所看准的市场和用户群也与之前的老一代 W ...
- 第五节:numpy之数组维度处理
- 【模板】Link-Cut Tree
#include<cstdio> #include<algorithm> #define N 500010 #define rg register #define ls (c[ ...
- 倍增/线段树维护树的直径 hdu5993/2016icpc青岛L
题意: 给一棵树,每次询问删掉两条边,问剩下的三棵树的最大直径 点10W,询问10W,询问相互独立 Solution: 考虑线段树/倍增维护树的直径 考虑一个点集的区间 [l, r] 而我们知道了有 ...
- BZOJ2730 矿场搭建 解题报告 点双联通分量
题意概述: 一张有向图,在其中设置一些关键点(即题目中的逃生出口),使得删除任意一个点之后其余点都可以到达至少一个关键点. 问至少需要设置多少中关键点,有多少种设置方法. 解析: 首先,这道题要求删掉 ...
- 虚拟机win7 自动休眠
1.问题描述:vmware 虚拟机,windows7系统.当一段时间不操作时,任务会休眠. 2.原因:win7系统自动休眠引起的. 3.解决方法:关闭win7系统的自动休眠. 依次打开“控制面板”-- ...
- NandFlash、NorFlash、DataFlash
1. NandFlash和NorFlash Flash存储芯片,俗称闪存,因其具有非易失性.可擦除性.可重复编程及高密度.低功耗等特点,广泛地应用于手机.数码相机.笔记本电脑等产品. ...
- [bzoj2850]巧克力王国_KD-Tree
巧克力王国 bzoj-2850 题目大意:给出n块巧克力,每块巧克力都有自己的两个参数x和y和本身的价值val,询问:m个人,每个人有两个系数和一个限度a,b,和c.求所有ax+by<=c的巧克 ...
- not in 和 <> 不走索引
首先我们要知道的一点就是CBO的代码oracle是不会对我们公开的,起码现在是.所以本文中的结论不一定适用所有的版本.在应用本文的结论之前最好先试一下. ok 下面就是本文的结论,当你在where语句 ...
- HTML_项目符号使用图片
本文出自:http://blog.csdn.net/svitter 创建一个HTML页面. 其内容为一个无序列表. 列表中至少包括了5本畅销书,每本书之前的项目符号必须採用概述封面的缩略图. 这些信息 ...