The first stage: Python Development Foundation function content summary outline

Defined functions

def 函数名():
    code
    return 返回值

Three methods defined functions

  1. There are function parameters
  2. No-argument function
  3. Empty function

Function return value

  1. return termination function
  2. The return value can return multiple values, stored in a tuple
  3. The default return None

Nonetype ....

Function call

Function name()

Function parameters

Katachisan

Description significance

Location parameter

One by one from left to right

The default parameter

Default values, the default parameter must be placed behind the position parameter

Arguments

It has a specific value

Argument position

A left to right pass a value to the parameter

Keyword argument

In the form of key-value pairs transmitted parameter; Keyword argument must be placed behind the position of the argument

Vararg

*

Katachisan

Receiving extra argument position, stored in the form of a tuple

Arguments

Broken tuple argument in the position to form a reference mass parameter

**

Katachisan

Keyword receiving extra arguments, stored as dictionary

Arguments

**{'a':1} --> a=1

Break up the dictionary, in the form of keyword arguments passed to the parameter (parameter have to have the parameter name key)

Function object

  1. Quote
  2. Function parameters
  3. Function's return value
  4. Container earth element

Function object variable name is the function name ==

Nested functions

def f1():
    def f2():
        pass

Namespace and scope

  1. Internal
  2. Overall situation
  3. Partial

Order of execution: Built -> Global -> partial

Search order: current -> Local -> Global -> Built-in

Global scope:

Local scope

  1. (X = 3) unrelated global scope (x = 1) and the local scope
  2. Local scope (x = 1) and the local effects of 1 (x = 3) and unrelated 2

Closure function

A variable x and the function within the function package B, then the function returns the value returned by the object A function of B

def B(x):
    # x = 1 
    def A():
        print(x)
        pass
    return A

Decorator

Function is used to add functionality, he is also a function of the nature of

  1. Without changing the source code of the function to be decorated
  2. It does not change the decorated function is called
def outter(func):
    def wrapper(*args,**kwrags):
        # 逻辑
        res = func(*args,**kwargs)  # func是被装饰的函数
        return res
    return wrapper

@outter
def index():
    pass



def sanceng(engine):
    def outter(func):
        def wrapper(*args,**kwrags):
            # 逻辑
            res = func(*args,**kwargs)  # func是被装饰的函数
            return res
        return wrapper
    return outter

@sanceng(engine)
def index():
    pass

Iterator

Iterables: iter objects have methods, the object may not necessarily be an iterative iterator object

Iterator object: iter and next object has methods, iterator object must be iterables, plus iter method iterator object itself or iterator

for circulation principle

for i in lt:

  1. The lt iterator object into
  2. Then use the next iteration method to get each element
  3. Capture aborted while loop

A triplet of expressions

print(1) if i > 10 else print(2)

List comprehensions

[I for i in range (10)] # 0-9 has come out

Dictionary of formula

{k:v for k,v in dic.items()}

Generator expressions

(I for i in range (10)) # next value not used, there is no value

Builder

Custom iterator

def func():

​ yield

yield:

1. 暂停函数,遇到下一个yield继续运行函数体代码
  1. It has a return value
  2. Let function () becomes a generator object

return:

1. 终止函数
  1. return value

Anonymous function

Function without a name parameter lambda: block

Generally not used alone, with max / min / map / filter / sorted MS

Recursion

It calls the function itself, but have to have a termination condition

Built-in functions

Is the direct use of built-in functions, the python interpreter belonging to the

Data type of built-in functions to use only the data type itself

enumerate()

Process-oriented programming

assembly line

Advantages: clear thinking

Disadvantages: poor scalability; not independent of function and function; trouble debugging

Guess you like

Origin www.cnblogs.com/foreversun92/p/11359938.html