Discuss / Python / 带参and不带参

带参and不带参

Topic source

import functools

def log(txt=None):

    if isinstance(txt,str):

        def log1(func):

            @functools.wraps(func)

            def w(*a,**kw):

                print('%s:%s'%(txt,func.__name__))

                func(*a,**kw)

                print('函数装饰之后')

            return w

        return log1

    else:

        @functools.wraps(txt)

        def w(*a,**kw):

            print('%s'%txt.__name__)

            txt(*a,**kw)

            print('函数运行完毕')

        return w

@log

def now():

    print('支持@log的装饰器测试')

@log('love')

def now1():

    print('支持log(\'love\')的装饰器测试')

now()

print('----------')

now1()


  • 1

Reply