0%

【Python笔记】装饰器

0、一切皆对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
def hello(str="Hello World!!!"):
print(str)


if __name__ == "__main__":
hello()
greet = hello
greet()

del hello
try:
hello()
except NameError:
print("There is no function named \"hello\"")
greet()
pass

0.1、在函数中定义函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
""" 在函数中定义函数 """


def function(str="Hello World!!!"):

def sub_function(str):
print(f"sub_function: {str}")

sub_function(str)
print(str)


if __name__ == "__main__":
function()
try:
sub_function()
except NameError:
print("There is no function named \"sub_function\"")
pass

0.2 、函数返回函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
""" 函数返回函数 """


def function(str="Hello World!!!"):

def sub_function(str):
print(f"sub_function: {str}")

sub_function(str)
print(str)
return sub_function


if __name__ == "__main__":
ret = function()
ret("sub function test.")

0.3、函数作为参数传递

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
""" 函数作为参数传递 """


def function(str="Hello World!!!"):

def sub_function(str):
print(f"sub_function: {str}")

sub_function(str)
print(str)
return sub_function


def test_function(func):
func("Call func, in test_function.")
pass


if __name__ == "__main__":
ret = function()
test_function(ret)

1、定义

​ python装饰器就是用于拓展原来函数功能的一种函数,这个函数的特殊之处在于它的返回值也是一个函数,使用python装饰器的好处就是在不用更改原函数的代码前提下给函数增加新的功能。

2、装饰器

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
""" 装饰器 """

from functools import wraps


def decorator(func):
""" @wraps接受一个函数来进行装饰,并加入了复制函数名称、注释文档、参数列表等等的功能。
这可以让我们在装饰器里面访问在装饰之前的函数的属性。 """

# @wraps(func)
def wrap_function():
print("Before exec func.")
return func()

return wrap_function


@decorator
def test_function():
print("In test_function.")
pass


if __name__ == "__main__":
test_function()
print("*" * 20)
print(test_function.__name__)
pass

3、带参数的装饰器

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
""" 带参数的装饰器 """


def logging(level):

def outwrapper(func):

def wrapper(*args, **kwargs):
print(f"[{level}]: {func.__name__}() function has been called")
return func(*args, **kwargs)

return wrapper

return outwrapper


@logging(level="INFO")
def hello(a, b, c):
print(f"a={a}, b={b}, c={c}")
pass


if __name__ == "__main__":
hello(1, 2, 3)
pass

4、类做装饰器

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
""" 类做装饰器 """


class DebugLogging(object):

def __init__(self, func):
self.func = func

def __call__(self, *args, **kwargs):
print(f"[DEBUG]: {self.func.__name__}() function has been called")
return self.func(*args, **kwargs)


@DebugLogging
def func_A(a, b, c):
print(f"a={a}, b={b}, c={c}")
pass


class Logging(object):

def __init__(self, level):
self.level = level
pass

def __call__(self, func):

def wrapper(*args, **kwargs):
print(
f"[{self.level}]: {func.__name__}() function has been called")
return func(*args, **kwargs)

return wrapper


@Logging("INFO")
def func_B(a, b, c):
print(f"a={a}, b={b}, c={c}")
pass


if __name__ == "__main__":
func_A(1, 2, 3)
print("*" * 20)
func_B(4, 5, 6)
pass