2019独角兽企业重金招聘Python工程师标准>>>

http://blog.csdn.net/fanshengchong/article/details/7039692

Python 的模块可以按目录组织为包

创建一个包的步骤是:

1. 建立一个名字为包名字的文件夹

2. 在该文件夹下创建一个__init__.py 文件

3. 根据需要再该文件夹下存放脚本文件、已编译扩展及子包

4. import pack.m1, pack.m2, pack.m3

5. 模块是一个可以导入的Python 脚本文件

6. 包是一堆按目录组织的模块和子包,目录下的 __init__.py 文件放了包的信息

7. 可以用import,import as,form import等语句导入模块和包

1, module 的实质

python 中的module 本质上就是一个相对独立的python文件.

当需要重用某些功能时, 将此功能所在的模块import到需要的模块即可.

2,  import 的实质

import的过程实际上包括两个主要部分,

1) 类似于shell 的 "source" 命令, 具体说即相当于将被import的module(即python文件) 在当前环境下执行一遍.

当多次import同一个module时, python会保证只执行一次. 参考Example1

2) import的第二个部分是使被import的成员(变量名, 函数名, 类....)可见.

为保证不发生命名冲突, 需要以 module.name 的方式访问导入的成员. 参考Example2

3, from *** import 的实质 (import 与 from *** import 的区别)

与import类似, 被导入的module仍然会执行且仅执行一次.

区别是当以 "from *** import " 方式导入module时, python会在当前module 的命名空间中新建相应的命名.

即, "from t2 import var1" 相当于:

import t2

var1= t2.var1

在此过程中有一个隐含的赋值的过程

由于python赋值操作特性(参考Python 学习之1: 变量名的本质),

在当前代码中对"var1"的修改并不能影响到t2.py中"var1"的值. 同时, 在t2.py中对var1的修改也不能反映到当前的代码.

参考Example3

Example1:

t1.py:

import t2
import t3
print "execution of t1.py"

t2.py:

print "execution of t2.py"

t3.py:

import t2
print "execution of t3.py"

(env) D:\test>python t1.py
execution of t2.py
execution of t3.py
execution of t1.py

Example2:

t1.py:

import t2

print "a varialbe in t2 is %s" % t2.var_in_t2
print "now will call a functiong in t2"
t2.func_in_t2

t2.py:

var_in_t2 = 0
def func_in_t2():
    print "this is a function in t2.py"
    
print "execution of t2.py"

(env) D:\test>python t1.py
execution of t2.py
a varialbe in t2 is 0
now will call a functiong in t2

Example3:

t1.py

from t2 import var_in_t2
import t2

print "the initial value in t1.py is %s" % var_in_t2
print "the initial value in t2.py is %s" % t2.var_in_t2

print "now try to change the value in the t1.py"
var_in_t2 = 50
print "now the value in t1.py is %s" % var_in_t2
print "now the value in t2.py is %s"  % t2.var_in_t2

print "now try to change the value in the t2.py"
t2.var_in_t2 = 20
print "now the value in t1.py is %s" % var_in_t2
print "now the value in t2.py is %s"  % t2.var_in_t2

t2.py

var_in_t2 = 0
def change_var():
    global var_in_t2
    var_in_t2 = 100

(env) D:\test>python t1.py
the initial value in t1.py is 0
the initial value in t2.py is 0
now try to change the value in the t1.py
now the value in t1.py is 50
now the value in t2.py is 0
now try to change the value in the t2.py
now the value in t1.py is 50
now the value in t2.py is 20

转载于:https://my.oschina.net/airship/blog/730824

Python 学习之二: module, import 与 import as相关推荐

  1. Python学习(二)列表,for循环,切片,元组

    文章目录 Python学习(二) 列表 访问列表元素 修改列表元素 在列表中插入元素 在列表末尾插入元素 在列表中插入元素 删除列表元素 使用del语句删除元素 使用`pop()`删除元素 根据值删除 ...

  2. python学习(二)

    Python学习(二) 前言:继续记录阅读和实践中遇到的问题 1.使用easygui模块,在消息提示框中输入汉字弹出的提示框乱码,解决方法:在代码前加入"#-*- coding=utf-8 ...

  3. Python学习(二)字符串与类型

    #Python学习(二) ##字符串与类型 在字符串之间是可以进行加法运算的,这样就会将多个字符串拼接为一个字符串: a = 'hello'+'world' 但是要注意不能和其他类型的变量进行运算,只 ...

  4. python学习笔记(二) 基本运算

    python学习笔记(二) 基本运算 1. 条件运算 基本语法 if condition1: do somethings1elif condition2: do somethings2else: do ...

  5. python课程与c+课程有什么不同-Python学习之二:Python 与 C 区别

    引自http://www.lxway.com/181844.htm 从开始看Python到现在也有半个多月了,前后看了Python核心编程和Dive into Python两本书.话说半个月看两本,是 ...

  6. [Python学习] 专题二.条件语句和循环语句的基础知识

            前面讲述了"专题一.函数的基础知识",而这篇文章讲述的Python的条件语句和循环语句的基础知识.主要内容包括:         1.条件语句:包括单分支.双分支和 ...

  7. (10.1)Python学习笔记二

    1.在项目工程中要模块化测试一个开发的功能,在测试通过后交付给项目组其他人员继续开发.要保证代码开发的性能和效率以及可扩展性. 2.项目工程中的文件夹分类要功能模块明确清晰,在python中引入某一个 ...

  8. python学习笔记(二十三) -- 多进程和多线程

    目录 多线程多进程的意义 多进程的使用 方式一(fork):  只能在Unix/Linux/Mac系统下执行,windows不可以 方式二(multiprocessing.Process): 全平台通 ...

  9. python学习笔记二

    1 正则 1-1 普通字符 s1 = 'asd25454655js6565askJ\nNKJLasd5165123' # 1 匹配单个大写英文字母 obj = re.compile('[A-Z]') ...

最新文章

  1. SpringBoot项目打包war部署到服务器去掉项目名所遇到的坑
  2. 政府数据集中异地备份概述
  3. 源码共享,希望一起互相学习
  4. 存储过程 insert
  5. 首发骁龙8 Gen1!联想陈劲:摩托罗拉edge X将给行业扔一颗大炸弹
  6. ERwin 正向工程
  7. putty-gns3
  8. 【IoT】创业指南:智能硬件产品原型设计指南
  9. 如何利用excel中的数据源制作数据地图
  10. 设计模式——工厂方法模式
  11. 数据库原理 | 第2章 关系运算习题
  12. GIT统计代码量及IDEA Statistic统计解析
  13. android切换皮肤,Android 应用更换皮肤实现方法
  14. Android系统定制开机logo和开机动画
  15. 搞笑新闻联播之老公岗位制度(中)铃声 搞笑新闻联播之老公岗...
  16. 【转载】手机快充的核心模块:ChargePump
  17. C++ 加号运算符重载
  18. 常见元素 – a元素
  19. Cookie | Cookie的理论基础、Cookie中常用的方法
  20. 4.1 行列式的定义

热门文章

  1. XML中CDATA及其字符实体的使用
  2. 2018年08月19日发烧诸事记
  3. SpringMVC源码系列:HandlerMapping
  4. stitching detail输出的dot图含义
  5. IOS中的JSON数据的解析
  6. bzoj3110: [Zjoi2013]K大数查询 【树套树,标记永久化】
  7. Activity启动模式详解
  8. 【权值分块】bzoj1503 [NOI2004]郁闷的出纳员
  9. 详解C#的数学类,Math,浮点数(上)
  10. 自己录制的Oracle 相关视频(陆续更新)