• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
菩提叶子
博客园    首页    新随笔    联系   管理    订阅  订阅
python 装饰器

1、什么是装饰器?

Python的装饰器本质上是一个嵌套函数,它接受被装饰的函数(func)作为参数,并返回一个包装过的函数。这样我们可以在不改变被装饰函数的代码的情况下给被装饰函数或程序添加新的功能

2、装饰器的基本用法

 

def func1(func):
    def newfunc():
        print(1)
        func()
        print(2)
    return newfunc

def func():
    print(3)


func = func1(func)
func()

 

3、@符号用法

def func1(func):
    def newfunc():
        print(1)
        func()
        print(2)
    return newfunc

@func1
def func():
    print(3)

func()

4、装饰器嵌套

def func1(func):
    def newfunc():
        print(1)
        func()
        print(2)
    return newfunc

def func2(func):
    def newfunc1():
        print(4)
        func()
        print(5)
    return newfunc1

@func1
@func2
def func():
    print(3)


func()

5、用装饰器扩展带有参数的原函数

def func1(func):
    def newfunc(who,where):
        print('程度开始前')
        func(who,where)
        print('程序开始后')
    return newfunc

@func1
def func(who, where):
    print("{}在{}吃饭".format(who, where))

func('张三','李四家里')

 6、用装饰器扩展带有参数和返回值的原函数

def func1():
    def newfunc(*args, **kwargs)
        print(1)
        res = func(*args, **kwargs)
        print(2)
        return res
    return newfunc


@func1
def func(*args, **kwargs):
    lst = []
    dic = {'name':"张三"}
    for i in args:
        print(i)
    for k,v in kwargs.items():
        if k in dic:
            strvar = dic[k] + 'aaa' + v +'bb'
            lst.append(strvar)
    return lst


lst = func('电影院',name = '12')

7、类装饰器扩展原函数

class Myclass():
    def __call__(self,func):
        return self.func2(func)


    def func1(func):
        def newfunc():
            print(1)
            func()
            print(2)
        return newfunc
    


    def func2(self, func):
        def newfunc1():
            print(3)
            func()
            func(4)
        return newfunc1


#方法一
@Myclass.func1
def func():
    print(5)
#方法二
Myclass()
def func():
    print(5)


func()

8、带有参数的函数装饰器

def outer(num)
    def newfunc(func):
        def newfunc1(self):
            print(1)
            func(self)
            print(2)



        def newfunc2(self):
            print(3)
            func()
            print(4)
        if num == 1:
            return newfunc1
        elif num == 2:
            return newfunc2

class Myclass():
    @outer(1)
    def func1(self):
        print(5)

obj = Myclass()
obj.func1()

9、带有参数的类装饰器

class Myclass1():
    id = 4445
    


    def __init__(self,num):
        self.num = num



    def __call__(self,cls):
        if self.num == 1
            return self.newfunc1(cls)
        else:
            return self.newfunc2(cls)



    def test(self):
        print(1)


    def newfunc1(self,cls):
        def newfunc():
            #为当前cls这个类添加属性
            cls.id = Myclass.id
            #为当前cls这个类添加方法
            cls.test = Mylass.test
            return cls()
        return newfunc


    def newfunc2(self,cls):
        def newfunc():
            if "run" in cls.__dict__:
                #调用类中方法
                res = cls.run()
                cls.run = res
                return cls()

        return newfunc

#参数1

@Myclass1(1)
class Myclass():
    def run():
        return "亢龙有悔"

obj = Myclass()
print(obj.id)
obj.test()

 

posted on 2022-10-25 18:12  菩提叶子  阅读(3234)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3