#!/usr/bin/env python3 # -*- coding:utf-8 -*- import functools def log(args): if isinstance(args,str): text=args def func1(func): @functools.wraps(func) def mk_log(*args,**kw): print('%s %s' % (text,func.__name__)) func(*args,**kw) print('end') return mk_log return func1 else: func = args @functools.wraps(func) def mk_log(*arg,**kw): print('begin call %s' % func.__name__) func(*arg,**kw) print('end call %s' % func.__name__) return mk_log @log def say_hello(name): print('hello %s' % name) @log('use') def say_hi(name): print('hi %s' % name) say_hello('Jack') print(say_hello.__name__) say_hi('Jack') print(say_hi.__name__)
执行结果:
begin call say_hello hello Jack end call say_hello say_hello use say_hi hi Jack end say_hi
Sign in to make a reply
黑猫警长的秋天
执行结果:
begin call say_hello hello Jack end call say_hello say_hello use say_hi hi Jack end say_hi