zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    【Android】【踩坑日记】RecyclerView获取子View的正确姿势
    【Thinking in Java】编写构造器时应注意:尽量避免调用其他非private方法
    为什么匿名内部类只能访问final变量【转】
    【Thinking in Java】类和对象的初始化过程
    【Thinking in Java】Java Callable的使用
    【算法与数据结构】二叉搜索树的Java实现
    【Thinking in Java】组合、继承和代理的区别
    【面试经历】腾讯一二面分享与总结
    【面试经历】腾讯、网易有道和阿里的笔试分享及自我总结
    《Unity預計算即時GI》笔记:三、Clusters和总结
  • 原文地址:https://www.cnblogs.com/aomi/p/7027384.html
Copyright © 2011-2022 走看看