partial
用于创建一个偏函数,将默认参数包装一个可调用的对象,返回结果也是可以调用的对象,偏函数可以固定原函数的部分参数,从而调用更简单。
实例展示:
from functools import partial
def test(input, *args, **kw)
print("input: ", input)
print("args: ", args)
print("kw: ", kw)
func = partial(test, 0, 1, 2)
print('===================')
func()
print('===================')
func(3, 4, 5)
print('===================')
func(a='python', b='test')
输出如下:
===================
input: 0
args: (1, 2)
kw: {}
===================
input: 0
args: (1, 2, 3, 4, 5)
kw: {}
===================
input: 0
args: (1, 2)
kw: {‘a’: ‘python’, ‘b’: ‘test’}
注释:*args表示将实参按位置传值,多余的值传给args,且以元组的方式呈现;**kw表示形参中按照关键字传值,多余的值都给kw,且以字典的方式呈现。
参考链接:https://blog.csdn.net/qq_1290259791/article/details/84930850
wraps
保证函数调用的便利性和代码的简洁性
函数中返回函数
def hi(name="yasoob"):
def greet():
return "now you are in the greet() function"
def welcome():
return "now you are in the welcome() function"
if name == "yasoob":
return greet
else:
return welcome
a = hi() # 返回函数的地址
print(a)
# outputs: <function greet at 0x7f2143c01500>
# 上面清晰地展示了`a`现在指向到hi()函数中的greet()函数
print(a())
# outputs: now you are in the greet() function
再次看看上面的代码。在 if/else 语句中,我们返回 greet 和 welcome,而不是 greet() 和 welcome()。为什么那样?这是因为当你把一对小括号放在后面,这个函数就会执行;然而如果你不放括号在它后面,那它可以被到处传递,并且可以赋值给别的变量而不去执行它。 你明白了吗?让我再稍微多解释点细节。
当我们写下 a = hi(),hi() 会被执行,而由于 name 参数默认是 yasoob,所以函数 greet 被返回了。如果我们把语句改为 a = hi(name = “ali”),那么 welcome 函数将被返回。我们还可以打印出 hi()(),这会输出 now you are in the greet() function。
将函数作为参数传递到另外一个函数
def hi():
return "hi yasoob!"
# 参数为函数
def doSomethingBeforeHi(func):
print("I am doing some boring work before executing hi()")
print(func())
doSomethingBeforeHi(hi)
# outputs:I am doing some boring work before executing hi()
# hi yasoob!
第一个装饰器
def a_new_decorator(a_func):
print('Flag')
def wrapTheFunction():
print("I am doing some boring work before executing a_func()")
a_func()
print("I am doing some boring work after executing a_func()")
return wrapTheFunction
def a_function_requiring_decoration():
print("I am the function which needs some decoration to remove my foul smell")
# 正常调用一个函数,返回该函数的结果
a_function_requiring_decoration()
# outputs: "I am the function which needs some decoration to remove my foul smell"
# 函数a_function_requiring_decoration 作为 函数 a_new_decorator的参数进行传递,返回值为
# 函数wrapTheFunction,即a_function_requiring_decoration被装饰
ss = a_new_decorator(a_function_requiring_decoration) # 调用a_new_decorator函数,输
# 出Flag,返回值为函数 wrapTheFunction
print(ss.__name__)
ss()
# outputs: Flag
# wrapTheFunction
# I am doing some boring work before executing a_func()
# I am the function which needs some decoration to remove my foul smell
# I am doing some boring work after executing a_func()
你看明白上述代码了吗?我们刚刚应用了之前学习到的原理。这正是 python 中装饰器做的事情!它们封装一个函数,并且用这样或者那样的方式来修改它的行为。现在你也许疑惑,我们在代码里并没有使用 @ 符号?那只是一个简短的方式来生成一个被装饰的函数。这里是我们如何使用 @ 来运行之前的代码:
使用装饰器标识修改上述代码:@(不加wraps)
def a_new_decorator(a_func):
print('Flag')
def wrapTheFunction():
print("I am doing some boring work before executing a_func()")
a_func()
print("I am doing some boring work after executing a_func()")
return wrapTheFunction
@a_new_decorator
def a_function_requiring_decoration():
print("I am the function which needs some decoration to remove my foul smell")
a_function_requiring_decoration()
# outputs: Flag
# I am doing some boring work before executing a_func()
# I am the function which needs some decoration to remove my foul smell
# I am doing some boring work after executing a_func()
print(a_function_requiring_decoration.__name__)
# Output: wrapTheFunction
这并不是我们想要的!Ouput输出应该是"a_function_requiring_decoration"。这里的函数被warpTheFunction替代了。它重写了我们函数的名字。幸运的是Python提供给我们一个简单的函数来解决这个问题,那就是functools.wraps。我们修改上一个例子来使用functools.wraps。
使用装饰器标识修改上述代码:wraps
from functools import wraps
def a_new_decorator(a_func):
print('Flag')
@wraps(a_func)
def wrapTheFunction():
print("I am doing some boring work before executing a_func()")
a_func()
print("I am doing some boring work after executing a_func()")
return wrapTheFunction
@a_new_decorator
def a_function_requiring_decoration():
print("I am the function which needs some decoration to remove my foul smell")
a_function_requiring_decoration()
# outputs: Flag
# I am doing some boring work before executing a_func()
# I am the function which needs some decoration to remove my foul smell
# I am doing some boring work after executing a_func()
print(a_function_requiring_decoration.__name__)
# Output: a_function_requiring_decoration
上述代码参考链接如下:
https://blog.csdn.net/yibuchen/article/details/80587810
https://www.runoob.com/w3cnote/python-func-decorators.html