zoukankan      html  css  js  c++  java
  • 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

     



  • 相关阅读:
    Could not find file '..inhibernate.cfg.xml'解决方法:
    图片轮播插件-carouFredSel
    C语言数组
    C语言字符串
    C语言指针基础
    python 多线程
    Andriod之Activity
    Java面向对象的编程
    C++模板编程
    关于思考
  • 原文地址:https://www.cnblogs.com/hany-postq473111315/p/12846888.html
Copyright © 2011-2022 走看看