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的更多相关文章

  1. python BeautifulSoup4 获取 script 节点问题

    在爬取12306站点名时发现,BeautifulSoup检索不到station_version的节点 因为script标签在</html>之外,如果用‘lxml’解析器会忽略这一部分,而使 ...

  2. python click module for command line interface

    Click Module(一)                                                  ----xiaojikuaipao The following mat ...

  3. Python框架、库以及软件资源汇总

    转自:http://developer.51cto.com/art/201507/483510.htm 很多来自世界各地的程序员不求回报的写代码为别人造轮子.贡献代码.开发框架.开放源代码使得分散在世 ...

  4. python工具的安装

    下载: python安装包:python-2.7.3.msi pywin32-218.win32-py2.7.exe setuptools安装包:setuptools-0.6c11.win32-py2 ...

  5. Embeding Python & Extending Python with FFPython

    Introduction ffpython is a C++ lib, which is to simplify tasks that embed Python and extend Python. ...

  6. Machine and Deep Learning with Python

    Machine and Deep Learning with Python Education Tutorials and courses Supervised learning superstiti ...

  7. Python在Windows下开发环境配置汇总

    最近比较关注学习Python方面的资料和课程,由于Python本身基本都是在Linux下开发,本人windows用习惯了初用Linux各种别扭啊. 下面将我在配置Windows环境下的禁言写出来,与大 ...

  8. python 操作mongodb数据库模糊查询

    # -*- coding: utf-8 -*-import pymongoimport refrom pymongo import MongoClient #创建连接#10.20.66.106clie ...

  9. [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 ...

随机推荐

  1. python爬虫10 | 网站维护人员:真的求求你们了,不要再来爬取了!!

    今天 小帅b想给大家讲一个小明的小故事 ... 话说 在很久很久以前 小明不小心发现了一个叫做 学习python的正确姿势 的公众号 从此一发不可收拾 看到什么网站都想爬取 有一天 小明发现了一个小黄 ...

  2. PAT 1106 Lowest Price in Supply Chain

    A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...

  3. WEB 移动端 CSS3动画性能 优化

    很多时候,我们在开发移动端的时候要使自己的网页兼容不同的机型,很多时候会采用CSS3动画,但是很多时候在安卓机下,动画明显会出现卡顿,很难看,那么这里我介绍几个CSS 属性进行硬件加速那么就会得到明显 ...

  4. Beetl学习总结(2)——基本用法

    2.1. 安装 如果使用maven,使用如下坐标 <dependency> <groupId>com.ibeetl</groupId> <artifactId ...

  5. fzu 2124

    #include<stdio.h> #include<queue> #include<math.h> #include<algorithm> #incl ...

  6. spring boot 中访问 REST 接口

    RestTemplate restTemplate = new RestTemplate(); Object result = restTemplate.getForObject("http ...

  7. nyoj_264_国王的魔镜_201311271800

    国王的魔镜 时间限制:3000 ms  |           内存限制:65535 KB 难度:1   描述 国王有一个魔镜,可以把任何接触镜面的东西变成原来的两倍——只是,因为是镜子嘛,增加的那部 ...

  8. thymeleaf模板引擎基础使用(转)

    刚好项目上用到这个模板引擎,记录以下基础用法. thymeleaf介绍 简单说, Thymeleaf是一个跟Velocity.FreeMarker类似的模板引擎,它可以完全替代JSP .相比其他的模板 ...

  9. DotNetBar.Bar作为容器使用的方法及Text更新原理

    DotNetBar.Bar作为容器使用的方法及Text更新原理                          老帅    一.容器用法   控件DevComponents.DotNetBar.Ba ...

  10. 卸载了 TortoiseGit,问题太多

    电脑里面同一时候安装TortoiseGit 和 TortoiseSVN,使用tortoiseGIT来跟踪git项目,有一个非常无语的问题,git status显示都是clean的.可是目录图标却始终显 ...