python learning modules - custom modules (a)

chapter Five

5.1 Custom module

Modular concept:

Some common functions py in a file, this file is called a module.

Meaning the module:

1. easy to manage. Let's deconstruct the program more clearly, to achieve re-use function;

2. To enhance development efficiency. As the saying goes ism, to avoid duplication of-create the wheel, using someone else to write the module implements certain functions, can greatly improve development efficiency.

Module Category:

The first: built-in modules, also known as the standard library. python comes, 200, each module also includes many features, such os, sys, time;

The second category: third-party modules, third-party libraries. Pip install modules required by the installation instructions, such as Django, BeautfulSoup the like, about 6,000 kinds;

The third category: the custom module. Our own definition in the project module.

5.1.1 import

1. Use

Syntax: import module name

Module can contain executable statements and function definitions, the purpose of these statements is to initialize the module, they only executed when import import statement first encountered in the module name. Optimization means of import is to be used at any point in the program, and in the execution module to execute a statement in the first loaded, it will not repeat the statements inside the module when the call back again.

2. The first module into the implementation of three things
  • Create a module name to the namespace named in memory
  • Implementation of this namespace code
  • By module name. The contents of the reference module inside

ps: Repeat the import references directly within your u good results that have been loaded

3. The module is introduced into a separate namespace

Each module is a separate namespace, defined functions in this module, the name of this module as a global name space, so when writing custom modules, do not worry about global custom module variables are in conflict with the user's global variables after import.

Example (pits):

#自定义模块aries.py,当作脚本运行时,会看到的调用change函数后name已经被改变了
name = 'aries'

def change():
    global name
    name = 'allen'

change()
print(name)  #输出 allen
#把aries.py当作模块在新的py文件中加载时
import aries     #加载aries模块
name = 'Jack'    #在本py文件下,创建全局变量name与aries模块里的name同名
aries.change()   #调用aries下的change函数
print(name)
#### 输出
allen    #第一次加载aries时,执行模块里的语句
Jack     #chang函数并没有改变本地py文件中name的值
4. From alias module

Syntax: Import module name as an alias

Advantages: The long module name simplified, easy to use; facilitate optimization of the spreading code and

#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()
The plurality of modules introduced
# 可以但是不推荐
import os,sys,json   

#推荐写法
# 易于阅读 易于编辑 易于搜索 易于维护
import os
import sys
import json

5.1.2 from...import...

1.from ... import ... use

Syntax: Module 1 name import function name, function name form 2 ...

It can also be used as aliases

2.from ... ... the difference between import and import of

Using from ... import ... sucked module name directly into the current namespace, so the current namespace, use a name on it, without adding the prefix

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

Benefits: easy to use the

Disadvantages: easily conflict with the current implementation of the file name.

3. from ... import *

All of the modules in the function py loaded into this document, is not recommended .

In the module can be used __all__to control the function which can be called

#在aries模块中,使用all可以把能被调用的函数写进去
__all__=['change','a_count']

Two functions 5.1.3 py files

Write a good py file has two purposes:

  • Script, a file is the entire program, to be executed
  • Module, stored in a pile of document function is introduced for use

python built-in global variables __name__:

  • When the file is executed as a script: name equal__main__
  • When a file is imported as a module: __ name__ equal to the module name

In what may be used in a module with the article to restrict the conditions of the function

if __name__ == '__main__':  
   # 在模块文件中测试chang函数
   # 此模块被导入时 __name__ == aries 所以不执行
   change()

5.1.4 module search path

Find the order:

Module already loaded in memory -> built-in module -> module included in the path sys.path

[Special note:] our custom module name should not have the same name and built-in module system

Import sys.path:
#首先制作归档文件:zip module.zip foo.py bar.py 
import sys
sys.path.append('module.zip')
import foo,bar

#也可以使用zip中目录结构的具体位置
sys.path.append('module.zip/lib/python')

#windows下的路径不加r开头,会语法错误
sys.path.insert(0,r'C:\Users\Administrator\PycharmProjects\a')

Guess you like

Origin www.cnblogs.com/jjzz1234/p/11091353.html