# 装饰器

函数自动调用，避免重写函数

```python
def funA(fn):
    print('A')
    fn()  # 执行传入的fn参数
    return 'fkit'


'''
下面装饰效果相当于：funA(funB)，
funB 将会替换（装饰）成 funA() 语句的返回值；
由于funA()函数返回 fkit，因此 funB 就是 fkit
'''


@funA
def funB():
    print('B')


# funA(funB) >print('A')>print('B')>return 'fkit'>print('fkit')
print(funB)

print('--' * 10)


# 取消装饰的定义
def funB():
    print('B')


print(funA(funB))
```

## 带参数的函数装饰器

```python
import time
from tqdm import tqdm


def statistic_time(func):
    import datetime
    start = datetime.datetime.now()

    def fun(*args, **kwargs):
        func(*args, **kwargs)

    end = datetime.datetime.now()
    print('函数:%s，耗时:%s' % (func.__name__, end - start))
    return fun


@statistic_time
def call(*args, **kwargs):
    bar = tqdm(range(10))
    for i in bar:
        bar.set_description('resault:' + str(args[0] + i + kwargs['fuck']))
        time.sleep(0.2)


if __name__ == '__main__':
    print(1)
    call(10, 2, 3, 2, 3, 3, fuck=90)
```

## dataclass

<https://www.jianshu.com/p/bea5c202cf85>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/python/zhuang-shi-qi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
