• python模块与包


    一、.py文件可以看做一个模块,模块类似其他语言中封装的类库

    模块分类:
      内置模块
      自定义模块
      第三方模块(需要安装才能使用)

    我想要使用cal.py中定义的函数,可以这样做

    cal.py源代码:

    #!/usr/bin/python
    #coding:utf-8
    
    def add( a, b ):
        return a + b

    import_test.py要使用add函数:

    #!/usr/bin/python
    #coding:utf-8
    
    import cal
    
    print cal.add( 10, 20 )

    二,内置属性__name__

    cal.py

    #!/usr/bin/python
    #coding:utf-8
    
    def add( a, b ):
        return a + b
    
    print __name__
    ghostwu@ghostwu:~/python$ python cal.py
    __main__
    ghostwu@ghostwu:~/python$ python import_test.py 
    cal
    30
    ghostwu@ghostwu:~/python$ 

    当执行cal.py本身时,__name__被解释成__main__, 通过模块引入方式执行时,别解释成文件名,这个特性有什么用?

    #!/usr/bin/python
    #coding:utf-8
    
    def add( a, b ):
        return a + b
    
    if __name__ == '__main__':
        print add( 100, 200 )
    ghostwu@ghostwu:~/python$ python import_test.py 
    30
    ghostwu@ghostwu:~/python$ python cal.py
    300

    这就是__name__的一个小作用,当一个模块(.py文件)被别的文件调用时,一般是想调用它里面定义的函数或者方法,我们可以通过__name__让外部模块调用方式,不会执行到类似print add( 100, 200 )这样的

    具体业务代码。只用到里面的函数或者方法定义。

    三、模块加载顺序:

    当前目录如果有同名的系统模块,那么当前目录的模块会被import,系统模块会被忽略,如:

     1 ghostwu@ghostwu:~/python/module$ ls
     2 import_test.py  string.py
     3 ghostwu@ghostwu:~/python/module$ cat string.py 
     4 #!/usr/bin/python
     5 #coding:utf-8
     6 
     7 def add( a, b ):
     8     return a + b
     9 ghostwu@ghostwu:~/python/module$ cat import_test.py 
    10 #!/usr/bin/python
    11 #coding:utf-8
    12 import string
    13 str = 'ghostwu'
    14 print string.capitalize( str )
    15 
    16 ghostwu@ghostwu:~/python/module$ python import_test.py 
    17 Traceback (most recent call last):
    18   File "import_test.py", line 8, in <module>
    19     print string.capitalize( str )
    20 AttributeError: 'module' object has no attribute 'capitalize'
    21 ghostwu@ghostwu:~/python/module$ ls -a
    22 .  ..  import_test.py  string.py  string.pyc

    在当前目录下,定义了一个同名的string模块( 指的是与系统的string模块同名 ),由于执行的时候,当前目录的模块被import了,所以识别不了系统string模块的方法capttalize.

    只要删除目录下的string.py string.pyc,就能正常import系统的模块 

    1 ghostwu@ghostwu:~/python/module$ ls -a
    2 .  ..  import_test.py  string.py  string.pyc
    3 ghostwu@ghostwu:~/python/module$ rm string.py string.pyc
    4 ghostwu@ghostwu:~/python/module$ ls -a
    5 .  ..  import_test.py
    6 ghostwu@ghostwu:~/python/module$ python import_test.py 
    7 Ghostwu

    四,当一个目录下有__init__.py文件,就可以当做一个包来用

     1 ghostwu@ghostwu:~/python/package_test$ pwd
     2 /home/ghostwu/python/package_test
     3 ghostwu@ghostwu:~/python/package_test$ ls -a
     4 .  ..  calc.py  calc.pyc  fab.py  fab.pyc  __init__.py  __init__.pyc
     5 ghostwu@ghostwu:~/python/package_test$ cat calc.py
     6 #!/usr/bin/python
     7 #coding:utf-8
     8 
     9 def add( a, b ):
    10     return a + b
    11 
    12 def sbb( a, b ):
    13     return a - b
    14 
    15 def mul( a, b ):
    16     return a * b
    17 
    18 def div( a, b ):
    19     return a / b
    20 
    21 oper = { '+' : add, '-' : sbb, '*' : mul, '/' : div }
    22 
    23 def mySwitch( x, o, y ):
    24     return oper.get( o )( x, y )
    25 
    26 ghostwu@ghostwu:~/python/package_test$ cat fab.py
    27 #!/usr/bin/python
    28 #coding:utf-8
    29 
    30 def fab( n ):
    31     if n == 0:
    32         return 1
    33     else:
    34         return n * fab( n - 1)

    在test.py中,引用包中的模块

     1 ghostwu@ghostwu:~/python/package_test$ cd ..
     2 ghostwu@ghostwu:~/python$ ls -a
     3 .  ..  module  package_test  test.py  tmp
     4 ghostwu@ghostwu:~/python$ cat test.py 
     5 #!/usr/bin/python
     6 #coding:utf-8
     7 
     8 import package_test.calc
     9 import package_test.fab
    10 
    11 print package_test.calc.add( 10, 20 )
    12 print package_test.fab.fab( 6 )
    13 ghostwu@ghostwu:~/python$ python test.py 
    14 30
    15 720
    16 ghostwu@ghostwu:~/python$ 

    导入模块的另外两种方式: 别名与直接导入方法

     1 ghostwu@ghostwu:~/python/package_test$ ls
     2 calc.py  calc.pyc  fab.py  fab.pyc  __init__.py  __init__.pyc
     3 ghostwu@ghostwu:~/python/package_test$ python
     4 Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
     5 [GCC 5.4.0 20160609] on linux2
     6 Type "help", "copyright", "credits" or "license" for more information.
     7 >>> import calc
     8 >>> calc.add( 10, 20 )
     9 30
    10 >>> import calc as c
    11 >>> c.add( 100, 200 )
    12 300
    13 >>> from calc import add
    14 >>> add( 1, 2 )
    15 3
  • 相关阅读:
    2021总结、2022展望
    新人报到
    第六次实验
    第五次实验
    第一次实验
    第三次实验
    第二次实验
    第四次实验
    实验七
    Tomcat10巨坑,软件不是越新越好。(用到tomcat9及以下就可以)
  • 原文地址:https://www.cnblogs.com/ghostwu/p/8612200.html
Copyright © 2020-2023  润新知