-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecorator_func.py
More file actions
58 lines (46 loc) · 1.29 KB
/
decorator_func.py
File metadata and controls
58 lines (46 loc) · 1.29 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = 'zifengw'
import functools
print('------------------------First one----------------------------')
def log(func):
@functools.wraps(func)
def wrapper(*args, **kw):
print('call %s():' % func.__name__)
return func(*args, **kw)
return wrapper
@log
def now():
print('2017-04-03')
now()
print(now.__name__)
print('------------------------Second one----------------------------')
def log(text):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kw):
print('%s %s():' % (text, func.__name__))
return func(*args, **kw)
return wrapper
return decorator
@log('execute')
def now():
print('2017-04-03')
now()
print(now.__name__)
print('------------------------Exercises----------------------------')
def log(text='call'):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kw):
print('%s %s begin:' % (text, func.__name__))
ret = func(*args, **kw)
print('%s %s end:' % (text, func.__name__))
return ret
return wrapper
return decorator
#@log('execute')
@log()
def now():
print('2017-04-03')
now()