装饰器补充,递归函数和二分法

装饰器补充

多层语法糖

# 判断print执行顺序
def outer1(func1):
    print('加载了outer1')

    def inner1(*args, **kwargs):
        print('执行了inner1')
        res1 = func1(*args, **kwargs)
        return res1

    return inner1


def outer2(func2):
    print('加载了outer2')

    def inner2(*args, **kwargs):
        print('执行了inner2')
        res2 = func2(*args, **kwargs)
        return res2

    return inner2


def outer3(func3):
    print('加载了outer3')

    def inner3(*args, **kwargs):
        print('执行了inner3')
        res3 = func3(*args, **kwargs)
        return res3

    return inner3


def outer4(func4):
    print('加载了outer4')

    def inner4(*args, **kwargs):
        print('执行了inner4')
        res4 = func4(*args, **kwargs)
        return res4

    return inner4


@outer1  # inner1 = outer1(inner2),inner1指代的函数体代码与普通变量名authentic_func一致
@outer2  # inner2 = outer2(inner3)
@outer3  # inner3 = outer3(inner4)
@outer4  # inner4 = outer4(authentic_func)
def authentic_func():
    print('from authentic_func')


authentic_func()
print(authentic_func)  # <function outer1.<locals>.inner1 at 0x000001ADDFF0E620>
图片名称

有参装饰器

有参装饰器目的仅是用来给装饰器传递额外的参数

def outer(verification_mode):
    print('loaded outer')

    def login_auth_func(func_name):
        print('loaded login_auth_func')

        def inner(*args, **kwargs):
            username = input(' please enter your username>>>:').strip()
            password = input('please enter your password>>>:').strip()
            if verification_mode == 'list':
                print("verify using a 'list'")
            elif verification_mode == 'dict':
                print("verify using a 'dict'")
            elif verification_mode == 'file':
                print("verify using a 'file'")
            else:
                print('其他验证方式')
                if username == 'admin' and password == 'pwd':
                    print('landed successfully')
            res = func_name(*args, **kwargs)
            return res

        return inner

    return login_auth_func


@outer('else')  # 先执行函数调用outer('dict'),返回值login_auth_func,然后再在执行语法糖结构@login_auth_func
def func():
    print('loaded func')


func()
图片名称

函数

递归函数的本质

函数在运行过程中直接或者间接的调用了自身

方式一,直接调用

import sys

sys.setrecursionlimit(500)
count = 0
def func():
    print('loaded func')
    global count
    count += 1
    print(count)
    func()


func()  # RecursionError: maximum recursion depth exceeded while calling a Python object

方式二,间接调用

import sys

sys.setrecursionlimit(500)
count = 0
def func():
    print('loading func')
    global count
    count += 1
    print(count)
    delete()
    print('loaded func')
def delete():
    print('loading delete')
    func()
    print('loaded delete')


delete()  # RecursionError: maximum recursion depth exceeded while calling a Python object
图片名称

实际应用

# 公司薪资待遇分为五个级别,一级最低每个级别之间相差5000,现只知道一级员工的工资为15000,请问五级员工和十八级员工工资多少
# calculate_salary(5) = calculate_salary(4) + 5000
# calculate_salary(4) = calculate_salary(3) + 5000
# calculate_salary(3) = calculate_salary(2) + 5000
# calculate_salary(2) = calculate_salary(1) + 5000
def calculate_salary(n):
    if n == 1:
        return 15000
    return calculate_salary(n - 1) + 5000


print(calculate_salary(5))
print(calculate_salary(18))
l1 = [11, [22, [33, [44, [55, [66, [77, [88, [99, [100]]]]]]]]]]
'''要求:循环打印出列表中每一个数字'''


def seek_num(list1):
    for item in list1:
        if isinstance(item, int):  # 判断数据是否属于整型
            print(item)
        else:
            seek_num(item)


seek_num(l1)

算法之二分法

二分法是算法里面最简单的一个,主要用来感受算法的强大功能

tuple1 = (19, 25, 30, 53, 69, 73, 99, 102, 120, 177, 211, 333, 548, 666, 888, 999, 1001, 1111, 1200, 2500)


# 查找一个数 888
def get_target(tuple1, target_num):
    # 考虑找不到的情况,tuple1不可能无限制二分
    if len(tuple1) == 0:
        print('查无此数')
        return
    middle_index = len(tuple1) // 2
    if target_num > tuple1[middle_index]:
        tuple1_left = tuple1[middle_index + 1:]
        print(tuple1_left)
        get_target(tuple1_left, target_num)
    elif target_num < tuple1[middle_index]:
        tuple1_right = tuple1[:middle_index]
        print(tuple1_right)
        get_target(tuple1_right, target_num)
    else:
        print('找到了', target_num)


get_target(tuple1, 888)  # (333, 548, 666, 888, 999, 1001, 1111, 1200, 2500);(333, 548, 666, 888);(888,);找到了 888
图片名称
posted @ 2022-03-21 20:06  一梦便是数千载  阅读(33)  评论(0编辑  收藏  举报