Advanced Function Summary

Closure function

def f1():
    x = 10
    def f2():
        print(x)  # 10
        
x = 1000
f1()  # 10
print(x)  # 1000

def f1():
    x = 10
    def f2():
        print(x)  # 10
    return f2

f = f1()  # f2

f()  # f2()
  • The packaged with functions and variables to be taken out
def f1(x):
    def f2():
        print(x)  # 10
    return f2

f3 = f1(10)  # f2
f3()  # f2()  # 10
f3()  # 10
f3()  # 10

f4 = f1(5)
f4()  # 5
f4()  # 5

Decorator

  • Does not change the function body code, and does not change the function call, it is essentially a function
def f1(x):
    def f2():
        print(x)  # 10
    return f2

f2 = f1()
f2()  # f2()
  • Perfect decorator
def login_deco(func):
    def wrapper(*args,**kwargs):
        login_judge = login()
        if login_judge:
            res = func(*args,**kwargs)
            return res
    return wrapper

@login_deco
def shopping():
    pass

# shopping = deco(shopping)
# shopping()

Three decorator

def sanceng(x,y):
    def login_deco(func):
        print(x,y)
        def wrapper(*args,**kwargs):
            login_judge = login()
            if login_judge:
                res = func(*args,**kwargs)
                return res
        return wrapper
   return login_deco

@sanceng(10,20)
def shopping():
    pass

day20
# shopping = login_deco(shopping)
# shopping()

Iterator

  1. Objects (Python in everything is an object) has iter method: iterables
  2. Iterator object: a method of and next iter
  • Iterator object must be iterator object is not necessarily iterables iterator object (f)

A triplet of expressions

List comprehensions

Dictionary of formula

Generator expressions

Builder

Since iterators defined internal functions use yield critical, as long as there is a function of the yield keyword call, after this call that function generator

  • yield: receiving a function return values, but will continue to function body
  • return: receiving a return value of the function, but the function will be terminated
def f1():
    yield 1
    
g = f1()  # 变成生成器

for i in g:
    print(i)  # 1

Recursion

The recursive function is essentially function calls itself, have to have a termination condition, and in a recursive process, the scale of the problem must be shrinking

Binary search

def find_num(num,lis):

    if len(lis) == 1 and lis[0] != num:
        print('没找到')
        return

    mid_ind = int(len(lis) / 2)  # 中间索引
    mid_num = lis[mid_ind]  # 中间值

    if num < mid_num:
        lis = lis[:mid_ind]
        find_num(num,lis)
    elif num > mid_num:
        lis = lis[mid_ind + 1:]
        find_num(num, lis)
    else:
        print('find')

lis = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
find_num(20,lis)

Anonymous function

lamdbda 参数 : 逻辑代码

  • Anonymous function becomes ineffective once, generally not used alone, and the maximum value max / min Min / sorted sorting / map mapping / filter filters used in conjunction
max(dic,key=lambda name: dic[name])
max(dic)

max(lis/se/tup)
  • Built-in functions (free look, no time to copy a copy)

Process-oriented programming

Similar to the factory assembly line, step by step to complete a mechanical item, particularly the completion of step segments interfere with each other between such steps are the

Disadvantages: poor scalability, as long as there is a step off, the project collapsed
advantages: clear and elegant

Guess you like

Origin www.cnblogs.com/nickchen121/p/11106249.html