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'>

最新文章

  1. tornado session
  2. BZOJ3522——[Poi2014]Hotel
  3. Android 保存图片到SQLite
  4. 巧用CSS3伪类选择器自定义checkbox和radio的样式
  5. NOIp 2014 #4 无线网络发射器选址 Label:模拟
  6. Knockoutjs实例 - 属性绑定(Bindings)之流程控制(Control flow)
  7. [ActionScript 3.0] AS3.0 Loader加载子swf时是否需要指定新的应用程序域ApplicationDomain
  8. 51nod1009(1的数目)
  9. coursera上的软件安全课程的课后阅读补充
  10. Got a packet bigger than &#39;max_allowed_packet&#39; bytes With statement Mysql终端数据同步不成功解决办法
  11. Spring源码情操陶冶-AbstractApplicationContext#postProcessBeanFactory
  12. requests+多进程poll+pymongo实现抓取小说
  13. 【自然语言处理篇】--以NLTK为基础讲解自然语⾔处理的原理和基础知识
  14. Web前端-Ajax基础技术(上)
  15. 【Django-URL name详解005】
  16. 华为Qinq的配置
  17. centos6.8配置php-fpm(php已在apache中以模块形式运行,nginx中同时以fastcgi运行)
  18. TCP 的那些事儿(上)(转)
  19. 线程event事件函数实现红绿灯
  20. 【工具类】怎么进入阿里云docker仓库

热门文章

  1. linux - 运维软件saltstack
  2. 198. House Robber(动态规划)
  3. vue 根据网站路由判断页面主题色
  4. hdu_1272_小希的迷宫_201403091527
  5. oracle 12c之前用sequence 和 trigger来生成自动增长的列
  6. iOS:改变UITableViewCell的选中背景色
  7. PHP array_combine()
  8. POJ2584_T-Shirt Gumbo(二分图多重最大匹配/最大流)
  9. C++ 句柄类的原理以及设计
  10. HDU 5289 Assignment (ST算法区间最值+二分)