day21 module

Type the code from the ordinary noodles, to the functional code is actually doing?

  1. The package code, a function similar lines 2-40, a small feature to achieve
  2. Let the different functions of the code to open an independent

Code Development History:

Noodles version - "Function Version -" File version - "folder version -" micro-services (to split large projects into smaller projects)

The file version, called modules in Python

  • Why should the module?

  • Module may put a lot of functions, then the number of functions is divided into a plurality of files, each module has a great function

Module

  • Module is ism, streamline operations, let us not repeat the same development features

    Classification Module

  1. Custom modules, write your own
  2. Third-party libraries, so you need to download, such as requests
  3. Built-in module, directly on the line, such as time
  4. Folder version, called the package in Python

Import, and is generally used import ... Import from ... module

# spam.py
print('welcome to spam.py')

def f1():
    print('from f1')

import 与 from...import

# test.py
import time 

1. 打开time文件
2. 使用Python解释器运行time文件,然后把解释文件内的名字放入模块time的名称空间,会运行文件中所有的代码
3. test.py中会有一个time变量指向time模块的名称空间,如果导入方式为`import time as t`,则是t变量指向time模块的名称空间
* 指向整个time文件,会把整个time文件都放入内存空间中,要用什么拿什么
优点:time里面有的方法全部拿出来
缺点:占用内存比较大,必须得通过time.出来
# test.py

from time import sleep

1. 打开time文件
2. 使用Python解释器运行time文件,然后把解释文件得到的名字放入time模块的名称空间,**同样运行所有的代码**
3. test.py中会有sleep变量指向time模块名称空间中的sleep,如果导入方式为`from time import sleep, localtime`,则是有一个sleep变量和一个locatime变量指向time模块名称空间中的sleep和localtime,`from time import *`同理,是有time中所有名字的变量
* 变量名直接指向A中的方法名,并没有指向A,是去A中把f1拿出来
* 一个是拿他的东西,另一个是拿他的店
优点:直接使用sleep就可以了
缺点:只能拿到sleep,如果该文件定义了sleep参数,则会冲突
### 导入多个模块
import spam, time, os

推荐下面的方法
import spam
import time
import os

Import circulation problems

  • Find another two files
# m1.py
from m2 import y
x = 1
print(y)

# m2.py
from m1 import x
y = 2
print(x)

Why circulation import questions

  1. Code run from top to bottom, m1 need y m2, so will the name space to find in m2 y, but before looking for y, will run the code m2,
  2. The first sentence m2 code is to look m1 of x, so the conflict being given
  3. That m1 and m2 are just running the first line, there have been introduced into circulation problem

A Solution

# m1.py
x = 10
from m2 import y
print(y)

# m2.py
y = 20
from m1 import x
print(x)
  • But that was very complex and need to be defined in advance, and the result is not what we want, palliatives

Solution two

# m1.py
def f1():
    from m2 import y
    print(y)
x = 1

f1()


# m2.py
def f2():
    from m1 import x
    print(x)
y = 2


f2()
  • Use of the name of the implementation (the definition of) order: Built-in (when the Python interpreter starts) - "Global (when the file execution) -" (when the function call) local

The module search path

1. to find memory

# test.py
import m1 # 从m1.py文件中导入的,然后会生成m1模块的名称空间
import time

# 删除m1.py文件,m1模块的名称空间仍然存在

time.sleep(10)

import m1  # 不报错,一定不是从文件中获取了m1模块,而是从内存中获取的

2. Built-in module to find

# time.py
print('from time')

# test.py
import time  # 无任何打印,所以他先去内置模块中找了

3. environment variable to find

import sys

print(sys.path)     # 打印当前父目录下环境变量中的包
# b/a/m1.py
# b/test.py

import m1   # 报错

sys.path.append('b/a')  # 把这个目录添加到环境变量中
import m1

Two uses Python file

  1. Module file, to be imported as a module, a plurality of
  2. Run the file, be treated as execution file is executed, only one
  • Search path to the executable file as a reference
# m1.py
def f1():
    print('from f1')
    
f1()

# test.py
import m1

m1.f1()      # 运行两次
  • __name__
# m1.py
def f1():
    print('from f1')
    
if __name__ == '__main__':  # __name__在m1.py被当做模块导入时是模块名,作为执行文件执行是'__main__'
    f1()
    
    
# test.py
import m1

m1.f1()     # 运行一次
  • After using the principle of eternal global name defined local name, before calling the function, variable has been defined well

Guess you like

Origin www.cnblogs.com/lucky75/p/10981263.html