• python modules


    1.什么是python的modules

    module指一个包含着python的定义和执行语句的文件。

    比如,一个文件:example.py,包含着可执行的py语句,则,这就是一个module,其名字就是example

    通过module,可以把很多大的文件,按照功能分割成小的文件,方便重用。

    比如,我们有一个文件add1.py,其内容是:

    def add(a, b):
       """This program adds two
       numbers and return the result"""
    
       result = a + b
       return result

     这里,我们就定义了一个module,叫做add1,其包含一个方法,就是add

    2.如何导入 module

    最简单的,我们使用import导入模块,比如import add1.可是,导入以后,还是不能直接使用add函数,而是要使用add1.add才可以。

    这里还需要注意一点,导入模块的时候,定义的函数等,不会被执行,但是,statements会被执行,比如,如下模块:

    print('This is a module for add')
    
    def add(a, b):
       """This program adds two
       numbers and return the result"""
    
       result = a + b
       return result

    其在被import的时候,print这一句,就会被执行。

    3.import 的时候,可以重新命名,比如:

    from w1 import add as wcfadd
    
    print(wcfadd(1,2))

    输出为:

    C:Python35python.exe C:/fitme/work/nltk/2.py
    This is a module for add
    3
    
    Process finished with exit code 0

    4.module在被import时候的寻找路径

    • The current directory.
    • PYTHONPATH (an environment variable with a list of directory).
    • The installation-dependent default directory.
    >>> import sys
    >>> sys.path
    ['',
    'C:\Python33\Lib\idlelib',
    'C:\Windows\system32\python33.zip',
    'C:\Python33\DLLs',
    'C:\Python33\lib',
    'C:\Python33',
    'C:\Python33\lib\site-packages']
    

    We can add modify this list to add our own path.

    5.重载(reloading)一个module

      python的解释器,在一个session里只载入某块一次,我们看一下这个函数,就是前面贴出的,这里修改一下;

    from w1 import add as wcfadd
    from w1 import add as wcfadd1
    from w1 import add as wcfadd2
    from w1 import add as wcfadd3
    from w1 import add as wcfadd4
    from w1 import add as wcfadd5
    
    print(wcfadd(1,2))
    print(wcfadd1(1,2))
    print(wcfadd2(1,2))
    print(wcfadd3(1,2))
    print(wcfadd4(1,2))
    print(wcfadd5(1,2))

    输出为:

    C:Python35python.exe C:/fitme/work/nltk/2.py
    This is a module for add
    3
    3
    3
    3
    3
    3
    
    Process finished with exit code 0

    如果需要重载,则:import.reload...

    6.import的时候,干了那几个事情:

      导入一个module
      将module对象加入到sys.modules,后续对该module的导入将直接从该dict中获得
      将module对象加入到globals dict中

    7.我们可以使用dir这个内建函数来查看module里的定义的names。

    print(dir())
    from w1 import add as wcfadd
    from w1 import add as wcfadd1
    from w1 import add as wcfadd2
    from w1 import add as wcfadd3
    from w1 import add as wcfadd4
    from w1 import add as wcfadd5
    print(dir())
    print(wcfadd(1,2))
    print(wcfadd1(1,2))
    print(wcfadd2(1,2))
    print(wcfadd3(1,2))
    print(wcfadd4(1,2))
    print(wcfadd5(1,2))

    输出是:

    C:Python35python.exe C:/fitme/work/nltk/2.py
    ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']
    This is a module for add
    ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'wcfadd', 'wcfadd1', 'wcfadd2', 'wcfadd3', 'wcfadd4', 'wcfadd5']
    3
    3
    3
    3
    3
    3
    
    Process finished with exit code 0
  • 相关阅读:
    C++闭包到C函数指针转化
    是否使用预编译头文件
    多线程模型一:只完成最新任务
    关于“函数针对入参判空并返回”
    C++函数参数的编写
    .Net Core(二) 下
    微信接口本地调试(IIS服务器)
    .Net Core 学习(二)上篇
    .Net Core学习(一)
    博客园的第一个博客
  • 原文地址:https://www.cnblogs.com/aomi/p/7027384.html
Copyright © 2020-2023  润新知