Discuss / Python / 2只能写出这样的,@log()要带个括号

2只能写出这样的,@log()要带个括号

Topic source

落汐21

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

import functools

def log(text=''): def deco(fn): @functools.wraps(fn) def wrapper(args,**kw): if(text!=''): print('%s %s %s():' % (text,'begin call',fn.name)) else: print('%s %s():' % ('begin call',fn.name)) return fn(args,**kw) return wrapper return deco

@log()

def f(): print('end call')

f()

@log('execute')

def g(): print('end call')

g()

Flames_CN

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

不对,题目里面是没要log后面的括号

幻夜201009

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

分情况,把需要参数的代码和不需要参数的代码都写一遍,if判断执行哪一个代码


  • 1

Reply