Python学习笔记(七)-- 装饰器

,[TOC]

1.何为装饰器?

简单来说,装饰器就是用来包装函数或类的“函数”,它接收一个对象(函数对象或类对象),并且返回一个对象(函数对象或类对象)。

2.装饰器如何使用?

用于包装函数的装饰器
首先给出一段代码,然后我们试着用包装器来包装它;

def squareSum(a, b):
    print("input", a, b)
    return a**2 + b**2

def squareDiff(a, b):
    print("input", a, b)
    return a**2 - b**2

print(squareSum(4,2))
print(squareDiff(4,2))

现在我们来尝试将上面的代码进行包装;
code1:无参数代码示例

#定义一个装饰器
def decorator(FUNC):  ##接收一个可调用对象FUNC
    #用来包装的代码段
    def hl_FUNC(a, b):
        print("input", a, b)
        return FUNC(a, b)
    return hl_FUNC  #返回一个可调用对象

@decorator  #注意这里,非常重要,等价于decorator(squareSum)
def squareSum(a, b):
    return a**2 + b**2

@decorator
def squareDiff(a, b):
    return a**2 - b**2


print(squareSum(4,2))
print(squareDiff(4,2))

code2:有参数代码示例

#新的装饰器包装层
def preString(pre = ''):  #新的装饰器的名称,并且有一个参数pre
    #旧的装饰器
    def decorator(FUNC):
        def hl_FUNC(a, b):
            print(pre + "input", a, b)
            return FUNC(a, b) 
         return hl_FUNC
     return decorator  #返回旧的装饰器的名称


@preString('sum +')  #注意跟无参的区别
def squareSum(a, b):
    return a**2 + b**2

@preString('diff -')
def squareDiff(a, b):
    return a**2 - b**2

print(squareSum(4,2))
print(squareDiff(4,2))

其实很简单,就是在旧的装饰器外面又包装了一层,并返回旧的装饰器;
用于包装类的装饰器
接收一个类对象,并且返回一个类对象;

def decorator(class_name):
    class class_hl:
        def __init__(self, footNum):
            self.displayNum = 0
            self.type = class_name(footNum)  #传入一个footNum参数
        def display(self):
            self.displayNum += 1;
            print("the times of display is: ", self.displayNum)
            self.type.display()
    return class_hl

@decorator  #注意,同样要带上这句,不能忘
class Animal:
    def __init__(self, footNum):
        self.footNum = footNum
    def display(self):
        print("the number of foot is: ", self.footNum)

cat = Animal(4)  #声明一个要传入装饰器的类对象,并初始化footNum
for i in range(5):
    cat.display()

打印结果:

the times of display is: 1
the number of foot is: 4
the times of display is: 2
the number of foot is: 4
the times of display is: 3
the number of foot is: 4
the times of display is: 4
the number of foot is: 4
the times of display is: 5
the number of foot is: 4
阅读终点,创作起航,您可以撰写心得或摘录文章要点写篇博文。去创作
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Python学习笔记》是由皮大庆编的一本关于Python语言学习的教材。在这本书中,作者详细介绍了Python语言的基础知识、语法规则以及常用的编程技巧。 首先,作者简要介绍了Python语言的特点和优势。他提到,Python是一种易于学习和使用的编程语言,受到了广大程序员的喜爱。Python具有简洁、清晰的语法结构,使得代码可读性极高,同时也提供了丰富的库和模块,能够快速实现各种功能。 接着,作者详细讲解了Python的基本语法。他从变量、数据型、运算符等基础知识开始,逐步介绍了条件语句、循环控制、函数、模块等高级概念。同时,作者通过大量的示例代码和实践案例,帮助读者加深对Python编程的理解和应用。 在书中,作者还特别强调了编规范和良好的编程习惯。他从命名规范、注释风格、代码缩进等方面指导读者如何出清晰、可读性强的Python代码。作者认为,良好的编程习惯对于提高代码质量和提高工作效率非常重要。 此外,作者还介绍了Python的常用库和模块。他提到了一些常用的库,如Numpy、Pandas、Matplotlib等。这些库在数据处理、科学计算、可视化等领域有广泛的应用,帮助读者更好地解决实际问题。 总的来说,《Python学习笔记》是一本非常实用和全面的Python学习教材。通过学习这本书,读者可以系统地学习和掌握Python编程的基础知识和高级应用技巧,为以后的编程学习和工作打下坚实的基础。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

eternity1118_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值