dryu999 发表于 2018-8-8 11:09:34

python模块-part1

  一:介绍
  模块定义:模块就是实现了某个功能的代码集合,一个模块可以定义函数,类和变量。模块还可以包括可运行的代码。
  优点:代码重用,可维护性高
  语法:
  导入单一模块:
import module_name  导入多个模块
import module_name1,module_name2,......  调用符号:.
import os  
print(os.sep)
  
print(os.getcwd())
  模块与文件关系:一个.py文件即一个模块,反之亦然。
对于一个模块test有如下定义:  

  
模块的文件名:test.py
  
模块名:test
  
模块导入:import test
  二:模块的名称空间(作用域)
  定义:名称空间就是一个从名称到对象的关系映射,每个模块都定义了自己的名称空间,即每个模块有自己独立的作用域。
  分类:python程序在运行时具有三个名称空间,内建(__builtin__模块),全局,局部,python解释器先加载内建名称空间,后加载全局名称空间,最后加载局部名称空间。
  名称空间与作用域关系:从内往外找,以较内层作用域为准
  示例一:
  module_a.py
x=1  

  
def test():
  
    print('from the module module_a')
  

  
module_b.py内容如下
  module_b.py
x=2  

  
def test():
  
    print('from the module module_b')
  test.py
import module_a  
import module_b
  

  
print(module_a.x)
  
print(module_b.x)
  
print(module_a.x+module_b.x)
  
module_a.test()
  
module_b.test()
  

  
运行结果:
  
1
  
2
  
3
  
from the module module_a
  
from the module module_b
  示例二:
  module_a.py
x=1  

  
def test():
  
    print('from the module module_a',x)
  test.py
import module_a  
x=2
  
print(child_module.x)
  
child_module.x=10
  
print(child_module.x)
  三:模块的搜索路径和路径搜索
  模块导入需要经历路径搜索的过程,路径搜索就是在你预定义的搜素路径里查找你想要导入的模块
  如果在预定义路径为找到,抛出异常(pycharm的特殊功能除外)
  自定义模块b在父级目录里(如有特殊需要可以使用sys.path.insert()方法)
  解决:
import sys,os  

  
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  

  
import b
  
b.test()
  四:from-import语句和as
  五:包
  定义:包就是一组模块的集合
  包与目录的区别:包必须包含一个空文件(也可以有内容)__init__
  六:阻止属性导入
  如果你不想让某个模块属性被 "from module import *" 导入 , 那么你可以给你不想导入的属性名称加上一个下划线( _ )。 不过如果你导入了整个模块或是你显式地导入某个属性这个隐藏数据的方法就不起作用了。
  示例一:
  module_test.py
def foo():  
    print('from the func foo')
  

  
def _bar():
  
    print('from the func bar')
  test.py
from child_module import *  

  
foo()
  
bar()
  

  
抛出异常:
  
NameError: name 'bar' is not defined
  示例二:
  module_test.py
def foo():  
    print('from the func foo')
  

  
def _bar():
  
    print('from the func bar')
  test.py
from child_module import _bar  

  
_bar()
  
正常运行
  七:导入循环(交叉引用)与import原理
  导入循环:两个模块互相导入
  import原理:将导入的模块执行一遍
  在python开发过程中,应尽量避免导入循环(交叉引用),但是,如果你开发了大型的 Python 工程, 那么你很可能会陷入这样的境地。
  解决方法:
  将 import 语句移到函数的内部,只有在执行到这个模块时,才会导入相关模块。
  错误示范:
  b.py
import os  
import a
  

  
def get_size(file_path):
  

  
    if a.file_exists(file_path):
  
      file_size=os.path.getsize(file_path)
  
      print('the file %s size is[%s]' %(file_path,file_size))
  a.py
import os  
import b
  

  
def file_exists(file_path):
  
    return os.path.isfile(file_path)
  

  
def download(file_path):
  
    b.get_size(file_path)
  
    print('download file %s' %file_path)
  

  
def upload(file_path):
  
    print('upload file %s' %file_path)
  

  
download('a.txt')
  解决方法:修改b.py
import os  

  
def get_size(file_path):
  
    import a
  
    if a.file_exists(file_path):
  
      file_size=os.path.getsize(file_path)
  
      print('the file %s size is[%s]' %(file_path,file_size))
  a.py不变
import os  
import b
  

  
def file_exists(file_path):
  
    return os.path.isfile(file_path)
  

  
def download(file_path):
  
    b.get_size(file_path)
  
    print('download file %s' %file_path)
  

  
def upload(file_path):
  
    print('upload file %s' %file_path)
  

  
download('a.txt')
页: [1]
查看完整版本: python模块-part1