Discuss / Python / 求教修饰器章节的习题思路

求教修饰器章节的习题思路

zsyNaCl

#1 Created at ... [Delete] [Delete and Lock User]

在修饰器章节的最后,习题如何解决? 我尝试了以下方式:

def log(text='excute'):
    def decorator(func):
        @functools.wraps(func)
        def wrapper(*args, **kw):
            print '%s start %s()' % (text, func.__name__)
            return func(*args, **kw)
        print '%s end %s()' % (text, func.__name__)
        return wrapper
    return decorator

但是结果是

excute end print_time()
call end print_time_b()
excute start print_time()
2015-01-19
call start print_time_b()
2015-01-19

求教这个print end如何加入 另外这个decorator在无参数调用的时候,只能使用@log(),而不是题目要求的@log,同求教。


  • 1

Reply