Supplementary function module

Built-in functions added:

map: map

map (func, iterables) ----> map objects

Each map will be the value of the iteration modify object, then the object map in a map, the map object may then be converted into lists / tuples,

Note that only one revolution (after the primary reason is to take the generator was taken over)

# Map map 
NAME_LIST = [ ' Egon ' , ' Jason ' , ' Sean ' , ' cake ' , ' Tank ' ]
map_obj = Map ( the lambda X: X + ' _dsb '  IF ! X = ' Tank '  the else X + ' like oysters ' , NAME_LIST)
 Print (map_obj)           # map_obj may become list / tuple type 
Print (List (map_obj))     # map_obj ----> generator (iterator) ---- "after use, can not get the 
Print (tuple (map_obj))

reduce: Merge

 When using the first call reduce module

from functools import reduce

Obtaining two values ​​from each iteration may be merged object, functions are performed reduce starts from an initial value of the combined

reduce (function address, iterables, the initial value (default 0))

# The reduce Merge

from functools import reduce
print(reduce(lambda x, y: x+y, range(1, 101), 0))

result:
5050

filter: filter

filer (function address, iterables) ----> filter objects

Results returned by the filter will function in the corresponding filter parameter value to True out, filtered off value is added to the filter object

xx_dsb.endswith ( '_ dsb'): Returns True or False

# Filter filtering 
NAME_LIST = [ ' egn_dsb ' , ' jason_dsb ' , ' sean_dsb ' , ' cake _dsb ' , ' Tank ' ]
filter_obj = filter ( the lambda name: name.endswith ( ' _dsb ' ), NAME_LIST)
 Print (filter_obj)
 Print (List (filter_obj))
 Print (tuple (filter_obj))       # empty tuple, has been taken over the list above

Recursive function (to know):

Recursive function refers to the repeated "direct or indirect" call the function itself, which is an expression of nested function calls form

Direct call: it refers to the built-in function, direct call function itself

Indirect calls: Calls between two mutually recursive functions indirectly caused

Learn:
Python recursive default depth: 998,1000 (limit the number of recursion)

However, each operating system will set the default recursion depth based on hard disk

import sys # acquisition module operating system resources

Get recursion depth: print (sys.getrecursionlimit ())

Set recursion depth: print (sys.setresursionlimit (2000)) (depth value)

# View the current value of the recursion depth

Note: There is no sense of time alone recursive call

Want recursive meaningful, must comply with two conditions:

- back:

  Refers repeatedly performed, the results of each execution must get closer to a result, there must be a termination condition backtracking

- Deduction:

  After backtracking to find a termination condition, start up step by step recursion

+ 2 = age4 age5         
age4 = 2 + Age3 
Age3 = Age2 +2 
Age2 = +2 AGE1 
AGE1 = 18 is   # back termination results 
#result = age (n-1) +2

From top to bottom is backtracking process, from the bottom up process is recursive

DEF Age (the n-):
      IF the n-== 1 :
           return 18
     return Age (the n--1) +2   # must write the return can be achieved recursive 
age ()

Module

What is a module?

Module is a combination of a series of functions

One module is essentially equivalent to the file # py wrapped stack of modules and function codes

Module Source:

1, Python built-in module (Python interpreter)

For example: sys / time / os / tutle

2, third-party modules (written by someone else)

For example: request

3, custom modules (write your own)

For example: you define demo.py file

Manifestations modules:
1, written in Python py file (understand)

2, compiled DLL or shared library in C or C ++ libraries (understanding)

3, following a set of packet py files with the _lnit_.py

-py_demo

-_init_.py

-demo.py

-demo2.py

What is the package?

Refers to the internal packet contains a folder __init__.py

py file in the Python interpreter:

1) file in the folder Python interpreter

2) one of the py file

Why use two modules?
Module can help us better manage the function codes, such as: function ...
may be split into a number of functional items were stored in different py files (modules) in.


Three how to create, write module and use the module?
- Right-click the file to create py
- py write python code file

- In a file by keyword import import module
import module name
# Note: When you import the module, the module can not add the suffix .py

- the use of the module stage, it must be noted, who is the executable file, who is import the file (module to be introduced)

- module on the first import, it has been fixed, the order of the current file is the first look to find memory

- what happens when you import the module:
1. will first execute the currently executing file and generate the executable file name space.
2. When the execution code into the module, the module is introduced will produce a module namespace.
3 will be introduced into the module namespace loaded into memory.

- to the module from the alias as
Import module as an alias module

- introducing a modular manner
- import module
- direct execution file import Imports

- from the package / module import module / (function name, variable name, class name)
- directly in the executable file import Import

- Import circulation problems:

- model1.py
from model2 import name
name = 'jason'

- model2.py
from model1 import name
name = 'tank'

 

- Import problem solving cycle:
1. The need to find the name on the import module above
2. Import Inside the function, the module becomes a function namespace name

- Software development directory specification:
Note: Every time writing project, must create a new folder with the project must be allowed to project folder as the project root directory.


- Project file folder
- conf:
- used to store configuration files folder
- Core:
- Core business code .py
- interface:
- Interface, within the interface logic to write code before data acquisition, after the adoption in order to obtain data
- db:
- used to store file data
- lib:
- public function to store files

- log:
- used to store the log file, a log for recording the user's operation

- bin:
- stored inside the boot file / - startup file

- the Readme.txt:
- project description, the user tells the operator the user program

 

 

 

Guess you like

Origin www.cnblogs.com/xy-han/p/11867236.html