Python-模块化

1、模块化

  一般来说,编程语言中,库,包,模块是同一个概念,是代码组织方式。

  Python中,只有一种模块对象类型,但是为了模块化组织模块的遍历,提供了‘包’的概念。

  模块 module,指的是Python的源代码文件。

  包package,指的是模块组织在一起的和包同名的目录及其相关文件。

2、导入语句 :

  2.1:import

  import 模块1[, 模块2 ]  ------完全导入,当然EPE8  推荐多个模块分别导入

  import... as....              -------模块别名

  import语句:

  1、找到指定的模块,加载和初始化它,生成模块对象,找不到,抛出importError异常

  2、在import所在的作用域的局部命名空间中,增加名称和上一步创建的对象关联。

  测试1

 1 print(dir())
 2 '''
 3 ['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'partial']
 4 
 5 '''
 6 
 7 import functools
 8 print(dir())
 9 print(functools)
10 print(functools.wraps)
11 
12 '''
13 ['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'functools', 'partial']
14 <module 'functools' from 'D:\\python3.7\\lib\\functools.py'>
15 <function wraps at 0x00000000022508C8>
16 '''
17 
18 import os.path
19 print(dir())
20 print(os)
21 print(os.path)
22 
23 '''
24 ['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'os', 'partial']
25 <module 'os' from 'D:\\python3.7\\lib\\os.py'>
26 <module 'ntpath' from 'D:\\python3.7\\lib\\ntpath.py'>
27 '''
28 
29 import os.path as osp
30 print(dir())
31 print(osp)
32 
33 '''
34 ['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'osp', 'partial']
35 <module 'ntpath' from 'D:\\python3.7\\lib\\ntpath.py'>
各自单独运行

    总结:

    1. 导入顶级模块,其名称会加入到本地名词空间中,并绑定到其模块对象。这里说的模块对象,就是导入的模块
    2. 导入非顶级模块,值将其顶级模块名称加入到本地名词空间中,导入的模块必须使用完全限定名称类访问。
      print(path)报错,
      print(os.path) 正确
    3. 如果使用了as,as后的名称直接绑定到导入的模块对象,并将该名称加入到本地名词空间中。

  2.2、form...import...

  from...import...  部分导入

  from...import... as... 别名

  测试2:

 1 from pathlib import Path, PosixPath # 当前名词空间导入该模块指定成员。
 2 print(dir())
 3 '''
 4 ['Path', 'PosixPath', '__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'partial']
 5 '''
 6 from pathlib import * # 在当前名词空间导入该模块所有公共成员(非下划线开头成员)或指定成员
 7 print(dir())
 8 '''
 9 ['Path', 'PosixPath', 'PurePath', 'PurePosixPath', 'PureWindowsPath', 'WindowsPath', '__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'partial']
10 '''
11 
12 from functools import wraps as wr, partial
13 print(dir())
14 '''
15 ['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'partial', 'wr']
16 '''
17 
18 from os.path import exists # 加载,初始化os, os.path模块, exists 加入bending名词空间并绑定
19 
20 if exists('f:/'):
21     print('found')
22 else:
23     print('not found')
24 
25 print(dir())
26 print(exists)
27 
28 '''
29 found
30 ['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'exists', 'partial']
31 <function exists at 0x0000000001DD1510>
32 
33 '''
34 import os
35 
36 print(dir())
37 print(os.path.exists)
38 # print(exists) # NameError: name 'exists' is not defined 除非 from os.path import exists
39 print(type(os.path)) # <class 'module'>
40 print(os.path.__dict__['exists'])
41 print(getattr(os.path, 'exists'))
42 
43 '''
44 ['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'os', 'partial']
45 <function exists at 0x0000000001DE1510>
46 <function exists at 0x0000000001DE1510>
47 <function exists at 0x0000000001DE1510>
48 '''
49 import  sys
50 print(sys.modules)
51 
52 '''
53 {'sys': <module 'sys' (built-in)>, 'builtins': <module 'builtins' (built-in)>, '_frozen_importlib': <module 'importlib._bootstrap' (frozen)>, '_imp': <module '_imp' (built-in)>, '_thread': <module '_thread' (built-in)>, '_warnings': <module '_warnings' (built-in)>, '_weakref': <module '_weakref' (built-in)>, 'zipimport': <module 'zipimport' (built-in)>, '_frozen_importlib_external': <module 'importlib._bootstrap_external' (frozen)>, '_io': <module 'io' (built-in)>, 'marshal': <module 'marshal' (built-in)>, 'nt': <module 'nt' (built-in)>, 'winreg': <module 'winreg' (built-in)>, 'encodings': <module 'encodings' from 'D:\\python3.7\\lib\\encodings\\__init__.py'>, 'codecs': <module 'codecs' from 'D:\\python3.7\\lib\\codecs.py'>, '_codecs': <module '_codecs' (built-in)>, 'encodings.aliases': <module 'encodings.aliases' from 'D:\\python3.7\\lib\\encodings\\aliases.py'>, 'encodings.utf_8': <module 'encodings.utf_8' from 'D:\\python3.7\\lib\\encodings\\utf_8.py'>, '_signal': <module '_signal' (built-in)>, '__main__': <module '__main__' from 'E:/code_pycharm/code_test_daye/a测试.py'>, 'encodings.latin_1': <module 'encodings.latin_1' from 'D:\\python3.7\\lib\\encodings\\latin_1.py'>, 'io': <module 'io' from 'D:\\python3.7\\lib\\io.py'>, 'abc': <module 'abc' from 'D:\\python3.7\\lib\\abc.py'>, '_abc': <module '_abc' (built-in)>, 'site': <module 'site' from 'D:\\python3.7\\lib\\site.py'>, 'os': <module 'os' from 'D:\\python3.7\\lib\\os.py'>, 'stat': <module 'stat' from 'D:\\python3.7\\lib\\stat.py'>, '_stat': <module '_stat' (built-in)>, 'ntpath': <module 'ntpath' from 'D:\\python3.7\\lib\\ntpath.py'>, 'genericpath': <module 'genericpath' from 'D:\\python3.7\\lib\\genericpath.py'>, 'os.path': <module 'ntpath' from 'D:\\python3.7\\lib\\ntpath.py'>, '_collections_abc': <module '_collections_abc' from 'D:\\python3.7\\lib\\_collections_abc.py'>, '_sitebuiltins': <module '_sitebuiltins' from 'D:\\python3.7\\lib\\_sitebuiltins.py'>, '_bootlocale': <module '_bootlocale' from 'D:\\python3.7\\lib\\_bootlocale.py'>, '_locale': <module '_locale' (built-in)>, 'encodings.gbk': <module 'encodings.gbk' from 'D:\\python3.7\\lib\\encodings\\gbk.py'>, '_codecs_cn': <module '_codecs_cn' (built-in)>, '_multibytecodec': <module '_multibytecodec' (built-in)>, 'types': <module 'types' from 'D:\\python3.7\\lib\\types.py'>, 'importlib': <module 'importlib' from 'D:\\python3.7\\lib\\importlib\\__init__.py'>, 'importlib._bootstrap': <module 'importlib._bootstrap' (frozen)>, 'importlib._bootstrap_external': <module 'importlib._bootstrap_external' (frozen)>, 'warnings': <module 'warnings' from 'D:\\python3.7\\lib\\warnings.py'>, 'importlib.util': <module 'importlib.util' from 'D:\\python3.7\\lib\\importlib\\util.py'>, 'importlib.abc': <module 'importlib.abc' from 'D:\\python3.7\\lib\\importlib\\abc.py'>, 'importlib.machinery': <module 'importlib.machinery' from 'D:\\python3.7\\lib\\importlib\\machinery.py'>, 'contextlib': <module 'contextlib' from 'D:\\python3.7\\lib\\contextlib.py'>, 'collections': <module 'collections' from 'D:\\python3.7\\lib\\collections\\__init__.py'>, 'operator': <module 'operator' from 'D:\\python3.7\\lib\\operator.py'>, '_operator': <module '_operator' (built-in)>, 'keyword': <module 'keyword' from 'D:\\python3.7\\lib\\keyword.py'>, 'heapq': <module 'heapq' from 'D:\\python3.7\\lib\\heapq.py'>, '_heapq': <module '_heapq' (built-in)>, 'itertools': <module 'itertools' (built-in)>, 'reprlib': <module 'reprlib' from 'D:\\python3.7\\lib\\reprlib.py'>, '_collections': <module '_collections' (built-in)>, 'functools': <module 'functools' from 'D:\\python3.7\\lib\\functools.py'>, '_functools': <module '_functools' (built-in)>, 'mpl_toolkits': <module 'mpl_toolkits' (namespace)>}
54 '''
测试

   总结:

    • 找到from子句中指定的模块,加载并初始化它(注意不是导入
    • 对于import子句后的名称
      1.  先查from子句导入的模块是否具有该名称的属性
      2. 如果不是,则尝试导入该名称的子模块 
      3. 还没有找到,就抛出ImportError异常
      4. 这个名称保存到本地名词空间总,如果有as 子句,则使用as子句后的名称。

         举例:   

from os import path

按照步骤:
    首先加载os模块,
    先查os下面有没有path属性,如果没有
    再找os下面有没有 path的模块,如果没有就报错
    找到后,就把这个名词,保存在本地名词空间中
    
          

    测试 3:

 1 # from pathlib import Path
 2 # print(Path, id(Path)) # 可以看到是 同一个对象。
 3 #
 4 # import pathlib as p
 5 # print(dir())
 6 # print(p)
 7 # print(p.Path, id(p.Path)) #  # 可以看到是 同一个对象。
 8 #
 9 # '''
10 # <class 'pathlib.Path'> 42273688
11 # ['Path', '__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'p', 'partial']
12 # <module 'pathlib' from 'D:\\python3.7\\lib\\pathlib.py'>
13 # <class 'pathlib.Path'> 42273688
14 # '''
产生同一个对象

     globals(), locals(), dir() 的作用范围总结1:

模块化:
    只用import 只能导入模块,不能是方法,或 类。

    from后面只能是module, import后面,类,函数, 变量

    导入之后不会消亡,即便是在函数中,函数消亡,也不会影响

    locals()----获取的是字典。!!!,可以获取变量对应的值说不定什么时候就用得着。

    14.08 视频

    import 一个模块,先到sys.module() 找,找到,就不用加载了,如果找不到在根据sys.path()找,再找不到该干嘛干嘛。

    dir()最大范围是所在模块。超不出它所在模块

    模块非常强的边界,就像国与国,调用必须通过模块,

    locals():
    globals():以模块为边界。
        dir():
            dir() 和locals() 是一样的。如果什么都不写

    使用了相对导入的摸块就不能当????17.00

    下滑线开头的模块名,还是想普通模块一样使用。私有的哪一类 只适合在类中使用,

    * 只能导入非下划线开头的

    

猜你喜欢

转载自www.cnblogs.com/JerryZao/p/9710572.html