## 一. 预备知识

``````def knights2(saying):
def inner2():
return "We are the knights who say: '%s'" % saying
return inner2
``````

``````a = knights2('Duck')
b = knights2('Hasenpfeffer')

print(a(), '---', a)
print(b(), '---', b)
``````

``````We are the knights who say: 'Duck' --- <function knights2.<locals>.inner2 at 0x0000026479031510>
We are the knights who say: 'Hasenpfeffer' --- <function knights2.<locals>.inner2 at 0x0000026479031598>
``````

## 二. 装饰器

``````def document_it(func):
def new_function(*args, **kwargs):
print('Running function:', func.__name__)
print('Positional arguments:', args)
print('Keyword arguments:', kwargs)
result = func(*args, **kwargs)
print('document_it Result:', result)
return result
return new_function
``````

``````def add_ints(a, b):
return a + b
``````

### 手工应用装饰器函数方式

``````>>> cooler_add_ints = document_it(add_ints)
Postitional arguments:(3， 5)
Keyword arguments: {}
document_it Result: 8
8
``````

• `cooler_add_ints = document_it(add_ints)`中，`document_it(add_ints)`调用返回了一个持有`add_ints`的闭包函数`new_function``cooler_add_ints`引用；
• `cooler_add_ints(3, 5)`调用时，参数`3``5`传给了`new_function`函数（实际上`cooler_add_ints(3, 5)`可以看成`new_function(3, 5)`）。然后，`new_function`再在自己内部用参数`3``5`调用第一步传进的`func`——即`add_ints`，生成结果。

### 使用装饰器名字来装饰函数

``````@document_it
return a + b
``````

``````>>> add_ints(3, 5)
Positional arguments: (3, 5)
Keyword arguments: {}
document_it Result: 8
8
``````

### 应用多个装饰器

``````def square_it(func):
def another_new_function(*args, **kwargs):
result = func(*args, **kwargs)
return result * result
return another_new_function
``````

``````>>> @document_it
... @square_it
... return a + b
...
Running function: another_new_function
Positional arguments: (3, 5)
Keyword arguments: {}
document_it Result: 64
64
``````

``````>>> @square_it
... @document_it
... return a + b
...
Positional arguments: (3, 5)
Keyword arguments: {}
document_it Result: 8
64
``````