python -

代码如下:

# coding=utf-8


def wrap(func):
    def call_it(*args, **kwargs):
        """wrap func: call_it"""
        print('before call')
        return func(*args, **kwargs)

    return call_it


@wrap
def hello():
    """say hello"""
    print("hello world")


from functools import update_wrapper


def wrap2(func):
    def call_it(*args, **kwargs):
        """wrap func: call_it2"""
        print('before call')
        return func(*args, **kwargs)

    return update_wrapper(call_it, func)


@wrap2
def hello2():
    """test hello"""
    print('hello world2')


if __name__ == '__main__':
    # __doc__ 获取函数的注释
    # __name__ 获取函数的名称
    hello()
    print('hello.__name__ = ', hello.__name__)
    print('hello.__doc__', hello.__doc__)

    print()
    hello2()
    print('hello2.__name__ = ', hello.__name__)
    print('hello2.__doc__', hello.__doc__)

输出:

before call
hello world
('hello.__name__ = ', 'call_it')
('hello.__doc__', 'wrap func: call_it')
()
before call
hello world2
('hello2.__name__ = ', 'call_it')
('hello2.__doc__', 'wrap func: call_it')

 

展开阅读全文

没有更多推荐了,返回首页