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
59
60
61
62
63
64
import functools


# 第一种
##################################################
def require_permission(permissions):
def decorated(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
has_permissions = permissions
if has_permissions:
return func(*args, **kwargs)
else:
print(403)

return wrapper

return decorated


@require_permission(True)
def foo():
print('foo')


@require_permission(False)
def foo():
print('foo')




# 第二种
#####################################################################
class require_permissions(object):
def __init__(self, permissions):
self.permissions = permissions

def __call__(self, fn):
@functools.wraps(fn)
def decorated(*args, **kwargs):

has_permissions = self.permissions

if has_permissions:
return fn(*args, **kwargs)
else:
print(403)

return decorated


def require_permission(permission):
return require_permissions(permission)


@require_permission(True)
def foo1():
print('foo')


if __name__ == '__main__':
foo()
foo1()