python闭包_装饰器

发布时间 2023-07-08 16:22:19作者: 虎虎生威啊

6.python高级知识-闭包装饰器/demo02_闭包.py

# -*- coding:utf-8 -*-

# 第一种方法================================
# 每次都更具汇率和钱都写一遍
# 这种方法是太烦了
print("第一种方法")
rate_usa = 0.7
money = 100
# print(rate_usa * money)
print("第一种方法结束")

# 第二种方法封装成函数================================
# 这种方法还是太烦
print("第二种方法")


def count_rate(rate, money):
    print(rate * money)


count_rate(100, 1000)
print("第二种方法结束")


# 第三种方法用缺省参数来代替汇率的输入================================
# 这种方法还是太烦
print("第三种方法")


def count_rate(money, rate=0.7):
    print(money * rate)


count_rate(100)
count_rate(100, 1000)
print("第三种方法结束")

# 第四种方法用全局变量来代替汇率的输入================================
# 这种方法还是太烦
print("第四种方法")
rate = 100


def count_rate(money):
    print(money * rate)


count_rate(100)
print("第四种方法结束")

# 第五种方法分装成类================================
# 这种方法魔法方法 太多还是太烦
print("第五种方法")


class CountRate(object):
    def __init__(self, rate):
        self.rate = rate

    def __call__(self, money):
        print(self.rate * money)


usa = CountRate(0.7)
usa(200)
rate_jp = CountRate(1000)
rate_jp(1000)

print("第五种方法结束")


# 第六种方法:闭包解决================================================
print("第六种方法")


def count(rate):
    def money(money):
        print(rate * money)

    return money


usa_rate = count(0.7)
usa_rate(100)

print("第六种方法结束")

6.python高级知识-闭包装饰器/demo06_装饰器_传递任意参数.py

# -*- coding:utf-8 -*-
def set_func(func):
    def call_fun(*args, **kwargs):
        return func(*args, **kwargs)

    return call_fun


@set_func
def test(data):
    return "test is show%s" % data


print(test(100))

6.python高级知识-闭包装饰器/demo03_闭包_函数引用.py

# 先写一个闭包
def set_fun(func):
    def call_fun():
        print('权限')
        func()

    return call_fun


def test():
    print("test is show")



# 把test函数引用传入闭包获取到内层函数的引用,将a指向内层函数的引用
a = set_fun(test)
# 调用内层函数
a()


6.python高级知识-闭包装饰器/demo08_类装饰器.py

# -*- coding:utf-8 -*-
class Func(object):
    def __init__(self, func):
        print('execute init function')
        self.func = func          # 类装饰器,会执行该类的init函数,然后传入的函数存储起来          

    def __call__(self, *args, **kwargs):
        print('execute call function')
        return self.func(*args, **kwargs)          # 然后会执行改类的call函数,在call函数里面直接调用传入的函数即可


@Func
def test(data):
    return "test is show%s" % data


print(test(100))

6.python高级知识-闭包装饰器/demo05_装饰器_传递参数.py

# -*- coding:utf-8 -*-
def set_func(func):
    def call_fun(data):
        func(data)

    return call_fun


@set_func
def test(data):
    print("test is show%s" % str(data))


test(100)

6.python高级知识-闭包装饰器/demo04_闭包_函数引用_语法糖_装饰器.py

# -*- coding:utf-8 -*-
# 先写一个闭包


def set_fun(func):
    def call_fun():
        print('权限')
        func()
    return call_fun


@set_fun  # 这个是语法糖:相当于test = set_fun(test)
def test():
    print("test is show")


# 调用内层函数
test()

6.python高级知识-闭包装饰器/demo01_函数_引用函数_函数当做参数.py

# -*- coding:utf-8 -*-
def test(data):
    print("test is show")


# 调用函数
test(123)


# 引用函数
ret = test

print(ret)
print(test)
print(ret==test)


# 通过引用调用函数
ret(123)


# 把函数引用当成参数传递
def application(func):
    func(123)


application(ret)

6.python高级知识-闭包装饰器/demo07_多个装饰器的执行顺序.py

# -*- coding:utf-8 -*-


def set_func1(func):
    print("set_fun1执行了")

    def call_fun(*args, **kwargs):
        print("call_fun1执行了")
        return func(*args, **kwargs)

    return call_fun


def set_func2(func):
    print("set_fun2执行了")

    def call_fun(*args, **kwargs):
        print("call_fun2执行了")
        return func(*args, **kwargs)

    return call_fun


@set_func2
@set_func1
def test():
    return "test is show"


print(test())