修饰器与语法糖:
什么是修饰器:
修饰器就是一个修饰的工具,比方说你花钱装饰你家,也叫装修。
在 python 中,修饰器:
- 修饰器就是修改其他函数的功能。
- 修饰器就是在保留函数原本功能的同时添加新的功能。
- 目的是优化代码,避免重复。
怎样理解修饰器:
- 理解1:修饰器就是一个闭包函数。
- 理解2:穿衣服:冬天穿羽绒服,夏天穿短袖。
如何使用修饰器:
修饰器的外层函数的参数一定是一个函数对象。
修饰器外层函数的返回值一定是内层函数的数据 / 对象。
语法糖:
关于 @ 的使用。
在要修饰的函数前一行 @ 修饰器。
举例:
用水果市场来举例讲解:
可想而知,一个水果市场具有以下功能:
- 查看自己列表中的水果。
- 为水果列表添加水果。
- 删除水果列表中的水果。
- 修改水果列表中的水果。
程序运行:
一个修饰器原型:
在修饰器中定义一个内部函数且返回值为该内部函数:
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