修饰器与语法糖

修饰器与语法糖:

什么是修饰器:

修饰器就是一个修饰的工具,比方说你花钱装饰你家,也叫装修。

在 python 中,修饰器:

  1. 修饰器就是修改其他函数的功能。
  2. 修饰器就是在保留函数原本功能的同时添加新的功能。
  3. 目的是优化代码,避免重复。

怎样理解修饰器:

  1. 理解1:修饰器就是一个闭包函数。
  2. 理解2:穿衣服:冬天穿羽绒服,夏天穿短袖。

如何使用修饰器:

修饰器的外层函数的参数一定是一个函数对象。

修饰器外层函数的返回值一定是内层函数的数据 / 对象。

语法糖:

关于 @ 的使用。

在要修饰的函数前一行 @ 修饰器。

举例:

用水果市场来举例讲解:

可想而知,一个水果市场具有以下功能:

  1. 查看自己列表中的水果。
  2. 为水果列表添加水果。
  3. 删除水果列表中的水果。
  4. 修改水果列表中的水果。

程序运行:

请添加图片描述

一个修饰器原型:

在修饰器中定义一个内部函数且返回值为该内部函数:

def decorator(fun):
    """这是一个装饰器:"""
    def inner(mlist):
        showInfo(mlist)
        fun(mlist)
        showInfo(mlist)
    return inner

正常调用模式:

正常调用时,不管进行那一步,都要调用 showInfo( ) 函数来展示水果列表,才能进行下一步操作。

这就造成了代码的重复。

def showInfo(mlist):
    # 展示水果列表:
    cnt = 1
    for i in mlist:
        print(f'{cnt}{i}')
        cnt += 1


def delete(mlist):
    # 删除指定水果:
    num = int(input("请输入你要删除水果的序号:"))
    del mlist[num - 1]


def replace(mlist):
    # 替换指定水果:
    num = int(input("请输入你要替换水果的序号:"))
    some = input("要替换为:")
    mlist[num - 1] = some


def addition(mlist):
    # 添加指定水果:
    some = input('请输入要添加的水果名称:')
    mlist.append(some)


fruits = ['苹果', '西瓜', '葡萄', '桃子', '香蕉', '荔枝']
while True:
    index = int(input("请选择你要进行的操作:\n 1.查看水果列表:\n 2.删除水果:\n 3.替换水果:\n 4.添加水果:\n 5.退出程序:"))

    if index == 1:
        # 查看水果:
        showInfo(fruits)

    if index == 2:
        # 删除水果:
        """正常调用函数:"""
        showInfo(fruits)
        delete(fruits)
        showInfo(fruits)

    if index == 3:
        # 替换水果:
        """正常调用函数:"""
        showInfo(fruits)
        replace(fruits)
        showInfo(fruits)

    if index == 4:
        # 添加水果:
        """正常调用函数:"""
        showInfo(fruits)
        addition(fruits)
        showInfo(fruits)
        
     if index == 5:
        break

修饰器模式:

优化了代码。

但是每次都需要向修饰器传入一个函数对象,并且还需要重新定义一个函数去接受返回的新函数。

def decorator(fun):
    """这是一个装饰器:"""

    def inner(mlist):
        showInfo(mlist)
        fun(mlist)
        showInfo(mlist)

    return inner


def showInfo(mlist):
    # 展示水果列表:
    cnt = 1
    for i in mlist:
        print(f'{cnt}{i}')
        cnt += 1


def delete(mlist):
    # 删除指定水果:
    num = int(input("请输入你要删除水果的序号:"))
    del mlist[num - 1]


def replace(mlist):
    # 替换指定水果:
    num = int(input("请输入你要替换水果的序号:"))
    some = input("要替换为:")
    mlist[num - 1] = some


def addition(mlist):
    # 添加指定水果:
    some = input('请输入要添加的水果名称:')
    mlist.append(some)


fruits = ['苹果', '西瓜', '葡萄', '桃子', '香蕉', '荔枝']
while True:
    index = int(input("请选择你要进行的操作:\n 1.查看水果列表:\n 2.删除水果:\n 3.替换水果:\n 4.添加水果:\n 5.退出程序:"))

    if index == 1:
        # 查看水果:
        showInfo(fruits)

    if index == 2:
        # 删除水果:
        """正常调用函数:"""
        # showInfo(fruits)
        # delete(fruits)
        # showInfo(fruits)

        """修饰器模式:"""
        fun1 = decorator(delete)
        fun1(fruits)

    if index == 3:
        # 替换水果:
        """正常调用函数:"""
        # showInfo(fruits)
        # replace(fruits)
        # showInfo(fruits)

        """修饰器模式:"""
        fun2 = decorator(replace)
        fun2(fruits)

    if index == 4:
        # 添加水果:
        """正常调用函数:"""
        # showInfo(fruits)
        # addition(fruits)
        # showInfo(fruits)

        """修饰器模式:"""
        fun3 = decorator(addition)
        fun3(fruits)

    if index == 5:
        break

语法糖模式:

最佳简化方案。

能达到相同的效果,但是步骤比较繁琐。

def decorator(fun):
    """这是一个装饰器:"""

    def inner(mlist):
        showInfo(mlist)
        fun(mlist)
        showInfo(mlist)

    return inner


def showInfo(mlist):
    # 展示水果列表:
    cnt = 1
    for i in mlist:
        print(f'{cnt}{i}')
        cnt += 1


@decorator
def delete(mlist):
    # 删除指定水果:
    num = int(input("请输入你要删除水果的序号:"))
    del mlist[num - 1]


@decorator
def replace(mlist):
    # 替换指定水果:
    num = int(input("请输入你要替换水果的序号:"))
    some = input("要替换为:")
    mlist[num - 1] = some


@decorator
def addition(mlist):
    # 添加指定水果:
    some = input('请输入要添加的水果名称:')
    mlist.append(some)


fruits = ['苹果', '西瓜', '葡萄', '桃子', '香蕉', '荔枝']
while True:
    index = int(input("请选择你要进行的操作:\n 1.查看水果列表:\n 2.删除水果:\n 3.替换水果:\n 4.添加水果:\n 5.退出程序:"))

    if index == 1:
        # 查看水果:
        showInfo(fruits)

    if index == 2:
        # 删除水果:
        """正常调用函数:"""
        # showInfo(fruits)
        # delete(fruits)
        # showInfo(fruits)

        """修饰器模式:"""
        # fun1 = decorator(delete)
        # fun1(fruits)

        """语法糖模式:"""
        delete(fruits)

    if index == 3:
        # 替换水果:
        """正常调用函数:"""
        # showInfo(fruits)
        # replace(fruits)
        # showInfo(fruits)

        """修饰器模式:"""
        # fun2 = decorator(replace)
        # fun2(fruits)

        """语法糖模式:"""
        replace(fruits)

    if index == 4:
        # 添加水果:
        """正常调用函数:"""
        # showInfo(fruits)
        # addition(fruits)
        # showInfo(fruits)

        """修饰器模式:"""
        # fun3 = decorator(addition)
        # fun3(fruits)

        """语法糖模式:"""
        addition(fruits)

    if index == 5:
        break

index == 4:
# 添加水果:
“”“正常调用函数:”“”
# showInfo(fruits)
# addition(fruits)
# showInfo(fruits)

    """修饰器模式:"""
    # fun3 = decorator(addition)
    # fun3(fruits)

    """语法糖模式:"""
    addition(fruits)

if index == 5:
    break
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

WhenYa

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

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

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

打赏作者

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

抵扣说明:

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

余额充值