Modules, custom modules

A module

  1. Definition: A module is a py file. This module stores a lot of similar features, similar to an aggregate function.

  2. Module Category:

    • Built-in module
    • Third party modules
    • Custom Modules
  3. import

    • import Import mean
  4. The first module into the implementation of three things

    • Tbjx.py to load files into memory.
    • Tbjx to create a namespace named in memory.
    • Et al reference the name of the module (variable and function names, class names, etc.) by name tbjx namespace
  5. It is introduced into a separate module namespace

    坑:通过tbjx.的方式引用此模块的名字时,一定一定是从此模块中寻找.
    
    通过import 引用模块 他有自己的独立名称空间,与当前执行文件没有关系.
    
    name = '李业'
    print(tbjx.name)
    
    def read1():
        print('in 02 模块import')
    
    tbjx.read1()
  6. Since the module alias

    • Benefits can be very long module name changed very short, easy to use.

      import tbjx as t
      t.read1()
    • It is conducive to the expansion and optimization of the code.

      #mysql.py
      def sqlparse():
          print('from mysql sqlparse')
      #oracle.py
      def sqlparse():
          print('from oracle sqlparse')
      #test.py
      db_type=input('>>: ')
      if db_type == 'mysql':
          import mysql as db
      elif db_type == 'oracle':
          import oracle as db
      db.sqlparse()
  7. Introducing a plurality of modules

    In one file, import multiple modules, a recommended wording is an import.

    import os,sys,json   # 这样写可以但是不推荐
    
    推荐写法
    
    import os
    import sys
    import json

二、from ... import ...

  1. using from ... import ...

    from ... import ... 的使用示例。
    
    from tbjx import name, read1
    print(name)
    read1()
    '''
    执行结果:
    from the tbjx.py
    太白金星
    tbjx模块: 太白金星
    ​
    '''
  2. from ... compared with the import ... import

    The only difference is: the use of spam from ... import ... sucked in the name directly into the current namespace, so the current namespace, use a name on it, without adding the prefix: tbjx.

    from import ... way ... we have advantages and disadvantages

    Benefits: easy to use the

    Disadvantages: easily conflict with the currently executing file name

    Example shows:

    There executable file variable or function name and the module of the same name, there will be coverage.

    name = 'oldboy'
    from tbjx import name, read1, read2
    print(name)  
    '''
    执行结果:
    太白金星
    '''
    ----------------------------------------
    from tbjx import name, read1, read2
    name = 'oldboy'
    print(name)  
    ​
    '''
    执行结果:
    oldboy
    ​
    '''
    ----------------------------------------
    def read1():
        print(666)
    from tbjx import name, read1, read2
    read1()
    ​
    '''
    执行结果:
    tbjx模块: 太白金星
    '''
    ----------------------------------------
    ​
    from tbjx import name, read1, read2
    def read1():
        print(666)
    read1()
    ​
    '''
    执行结果:
    tbjx模块: 666
    '''

    The current location directly read1 and read2 like, when executed, still tbjx.py global name space file

    #测试一:导入的函数read1,执行时仍然回到tbjx.py中寻找全局变量 'alex'
    #test.py
    from tbjx import read1
    name = 'alex'
    read1()
    '''
    执行结果:
    from the spam.py
    spam->read1->name = '太白金星'
    '''
    ​
    #测试二:导入的函数read2,执行时需要调用read1(),仍然回到tbjx.py中找read1()
    #test.py
    from tbjx import read2
    def read1():
        print('==========')
    read2()
    ​
    '''
    执行结果:
    from the tbjx.py
    tbjx->read2 calling read
    tbjx->read1->tbjx 'barry'
    '''
        4.3.3 也支持as
    
    通过这种方式引用模块也可以对模块进行改名。
    
    from tbjx import read1 as read
    read()
  3. Introducing a plurality of line

    from tbjx import read1,read2,name
  4. from ... import *

    from spam import * in all the tbjx not an underscore (_) at the beginning of the name are imported into the current position

    In most cases our python program should not be used to import this way, because * you do not know what name to import, the name is likely to be overwritten before you have defined. And extremely poor readability, there is no problem when importing in an interactive environment.

    You can use all controls * (used to release a new version), the new line in tbjx.py

    __all__=['money','read1'] #这样在另外一个文件中用from spam import *就这能导入列表中规定的两个名字

Third, the two functions py files

  1. Writing good a python file may have two purposes:
    a: a script, a file is the entire program, to be executed (such as simulation before you write that blog Login garden work, etc.)
    II: module, file stored in a pile function, to be imported using python for us built a global variable __name__, when the file is executed as a script: name equal to the ' main ' when the file is imported as a module: __ name__ equal to the module name # function: to .py file execution control different logical (or test code module file) in different application scenarios





    if __name__ == '__main__':![img]
    print('from the tbjx.py')
    
    __all__ = ['name', 'read1',]
    
    name = '太白金星'
    
    
    def read1():
       print('tbjx模块:',name)
    
    
    def read2():
       print('tbjx模块')
       read1()
    
    
    def change():
    
       global name
       name = 'barry'
    
    if __name__ == '__main__':  
       # 在模块文件中测试read1()函数
       # 此模块被导入时 __name__ == tbjx 所以不执行
       read1()

Fourth, the module search path

  1. Looking module path: Memory ----> Built-in module ---> sys.path looking in

    As long as these three places: sys.path memory built-in modules can be found in the module reference path, this module should be a direct reference to

  2. Manually add the path to reference other modules

    ​ sys.path.append(r'D:\s23\day15')

Guess you like

Origin www.cnblogs.com/cuixiaoying/p/11104814.html