Yi Shi Lu

Module basis

A module of four forms

1.1 What is a module

Module is a collection of a series of functions, because the function is a collection of functions, the module can be seen as a collection of a series of functions.

A file has a lot of internal functions, so a file can be seen as a module.

The name of a python file is XXX.py, then the module name is XXX.

Four forms module 1.2

  1. Custom modules: write a python own use certain features of files, can be called a custom module.
  2. Third-party modules: Module someone else has written some functions need to install their own use, there are third-party modules + 13w of the.
  3. Built-in module: python interpreter module comes.
  4. Package: The combination of a series of modules together into the evening, there __inti__ file folder.

1.3 Why Module

a python module library open the library, you can upload modules with different functions, direct use of ready-made modules, to improve development efficiency.

1.4 How to use the module

Using the import and generally ... Import from ... import module.

Two, import and from ... import

2.1 import

import first import will first create a module to module, whichever namespace in the corresponding file execution module, the name of the implementation process are generated into the name space module, a module name to get the last execution in the current document .

Before the creation of the module is repeated import, it will directly use good results, and will not repeat the file, import multiple modules, use a comma separated.

2.2 from...import

from ... import for the first time to import the module will first create whichever is the name of a module of the space, the file execution module corresponding to the name of the implementation process are generated into the name space module, the name of the executable file in the current space get in a name, the name of the module directly to a particular name, which means you can not add any prefix directly.

When using from ... import, without prefix, the code is simple, but prone to conflict with the currently executing file namespace name.

2.3 Similarities and Differences

Similarities: 1. Both modules will execute the corresponding file, the name space will have two modules. 2. When both calling function to find the relationship between the scope, and location-independent call when need to go to define

Different points: import needs prefix; from ... import ... no need to add the prefix

Third, the circulation import questions

Reuse modules one to another, when introduced prone cycle, makes it impossible to import, thus being given, for example:

# m1.py
print('from m1.py')
from m2 import x

y = 'm1'
  1. Creating m2 namespace
  2. Execution m2.py, the name of the generated execution thrown m2.py
  3. Get m2.x execution in the current document
# m2.py
print('from m2.py')
from m1 import y

x = 'm2'
  1. Creating m1 namespace
  2. Execution m1.py, the name of the generated execution thrown m1.py
  3. Get m1.y execution in the current document
# run.py
import m1
  1. Creating m1 namespace

  2. Execution m1.py, the name of the generated execution thrown m1.py

  3. M1 get the currently executing file

    If you run run.py, it will errorImportError: cannot import name 'y'

    If you run m1.py, it will errorImportError: cannot import name 'x'

    If you run m2.py, it will errorImportError: cannot import name 'y'

Solutions

We can use the function syntax definition phase only identifying characteristics of problem-solving cycle of import, we can also solve the problem of circulation imported from the essence, but the best solution is not to appear import cycle.

# m1.py
print('from m1.py')


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

x = 'm2'
from m1 import y

Fourth, the module search path

4.1 search module path

Module is actually a file, if you want to execute the file, first of all need to find the path to the module (a folder). If the module file path and execute files are not under the same file directory, we need to specify the module's path.

  1. Start with the memory module has been imported looking
  2. Find the built-in module
  3. Environment variable to find

4.2 to execute the search path document shall prevail

Five, random module

import random
print(random.random()) #大于0小于1的小数
0.03833739951277726
import random
print(random.randint(1,5))#[1,5]的整数
2
import random
print(random.rangrage(1,5))#(1,5)的整数
3
import random
print(random.uniform(1,3))#(1,3)小数
2.13548941567856
import random
print(random.choice([1,'32',[5,6]])#列表内的任意一个元素
1
import random
print(random.sample([1,'32',[5,6]])
['23', 1]
lis = [1, 3, 5, 7, 9]
# 打乱l的顺序,相当于"洗牌"
random.shuffle(lis)
print(lis)
[9, 1, 5, 7, 3]

Guess you like

Origin www.cnblogs.com/tangceng/p/11366068.html
lu