9.25 Summary function

Defined functions

def 函数名():
    pass

Three definitions way function

Empty function

def f():
    pass

There are function parameters

def f(x):
    print(x)

No-argument function

def f():
    print()

Call functions

函数名()

Function's return value

1.return return values
2.return function may be terminated
3.return can return multiple values returned tuple

Function parameters

Katachisan

Location parameter

Receiving position from left to right argument

The default parameter

Has a default value, must be placed behind the position parameter

Arguments

Argument position

From left to right position to pass parameter values

Keyword argument

According to the parameter name to the value argument must be back in position

Vararg

*

* Katachisan

Receiving extra argument position, receiving tuple

*Arguments

Iterables beaten, as the position parameter arguments passed

**

** parameter
receiving excess keyword arguments, receiving Dictionary

** arguments
break dictionary as keyword arguments passed to the parameter

* ** parameter parameter
receives all the extra parameters

Function object

def func():
    pass

Quote

f1 = func

Earth element as a container

lt = [func]

As a function return value

def f2():
    return func
f3 = f2()

As a function parameter

def f2(func):
    pass
f2(func)

Nested functions

def f1():
    def f2():
        pass

Namespace and scope

Built-in namespace

Built-in functions

Global name space

In addition to built-in and local, and the rest are global

The local name space

Internal functions

Execution order

Built → → local Global

Search Order

Current position: the local built → → → global error

Global scope

Not linked to global and local

Local scope

No local contact within the local foreign

global

Let the global and local conversion

nonlocal

Let the conversion within the local and topical

Variable data types will break the scope of relations

Closure function

def a(x):
    def b():
        print(x)
    return b
c = a(100)
c()

Decorator

1 is essentially a function of
2 does not modify the source code
3 does not change is called

Layer decorator

Layer decorator template

def deco(func):
    def wrapper(*args,**kwargs):
        res = func(*args,**kwargs)
        return res
    return wrapper
    
@deco
def index()
    pass

Three decorator

Iterator

Iterables

→ method comprising addition number iter

Iterator object

Iter method of containing and next → only file

A triplet of expressions

List comprehensions

Dictionary of formula

Generator expressions

g = (i for i in range(10)
print(next(g)) ##g.__next__()

Builder

It contains a function of yield

yield

1. Suspend function
2. A next can get a yield value

Recursion

It calls the function itself, there must be an exit condition

Anonymous function

lambda 参数:返回值

Built-in functions

enumerate Gets the index value +

Process-oriented programming

Similar lines

Guess you like

Origin www.cnblogs.com/793564949liu/p/11588240.html