functools函数中的partial函数及wraps函数

'''
partial引用函数,并增加形参
'''

import functools
def show_arg(*args,**kwargs):
    print("args",args)
    print("kwargs",kwargs)

q = functools.partial(show_arg,1,2,3)#1,2,3为默认值
# functools.partial(函数,形式参数)
q()#相当于将show_arg改写一下,然后换一个名字
q(4,5,6)#没有键值对,kwargs为空
q(a='python',b='Hany')
# 增加默认参数
w = functools.partial(show_arg,a = 3,b = 'XiaoMing')#a = 3,b = 'XiaoMing'为默认值
w()#当没有值时,输出默认值
w(1,2)
w(a = 'python',b = 'Hany')

import functools
def note(func):
    "note function"
    @functools.wraps(func)
    #使用wraps函数消除test函数使用@note装饰器产生的副作用 .__doc__名称 改变
    def wrapper():
        "wrapper function"
        print('note something')
        return func()
    return wrapper
@note
def test():
    "test function"
    print('I am test')
test()
print(test.__doc__)

2020-05-08

 



posted @ 2020-05-08 00:25  CodeYaSuo  阅读(152)  评论(0编辑  收藏  举报