-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
41 lines (36 loc) · 1.13 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import functools
#在函数的开头打印begin calls和结尾打印end calls
# def log(func):
# @functools.wraps(func) ##相当于赋值func.__name__=now.__name__,防止函数名字改变
# def wrapper(*args,**kw):
# print('begin calls')
# ee=func(*args,**kw) ##把ee赋值给func(*args,**kw),
# print('end calls') ##最后返回的也只调用ee,不会再调用func,在最后再打印一行出来
# return ee
# return wrapper
# @log
# def now():
# print('dasd-1dsad-12')
# now()
#写一个@log既支持 @log,也支持@log('excute')
#可选参数
# def log(text='call'):
# def decorator(func):
# @functools.wraps(func)
# def wrapper(*args,**kw):
# print('%s %s()' %(text,func.__name__))
# return func(*args,**kw)
# return wrapper
# return decorator
def log(arg='call'):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kw):
print('%s %s()' % (arg, func.__name__))
return func(*args, **kw)
return wrapper
return decorator
@log
def ttt():
print('asdas')
ttt()