> For the complete documentation index, see [llms.txt](https://l1nwatch.gitbook.io/interview_exercise/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://l1nwatch.gitbook.io/interview_exercise/python-yu-yan-te-xing/6-mian-xiang-qie-mian-bian-cheng-aop-he-zhuang-shi-qi.md).

# 6 面向切面编程AOP和装饰器

```python
#!/bin/env python3
# -*- coding: utf-8 -*-
# version: Python3.X
""" 练习一下自己怎么写一个装饰器

完整资料参考: https://taizilongxu.gitbooks.io/stackoverflow-about-python/content/3/README.html
"""

__author__ = '__L1n__w@tch'


def no_argument_decorator(function):
    # 注意这是在脚本被解释期间(还没进入 main 代码就在跑的了)
    print("I am a 无参数修饰器")

    def wrapper():
        print("{} 装饰开始 {}".format("*" * 30, "*" * 30))
        function()
        print("{} 装饰结束 {}".format("*" * 30, "*" * 30))

    return wrapper


def decorator_maker_with_arguments(decorator_arg1, decorator_arg2):
    print("创建装饰器, 同时接收参数: {}, {}".format(decorator_arg1, decorator_arg2))

    def my_decorator(func):
        print("装饰器, 得到参数: {}, {}".format(decorator_arg1, decorator_arg2))

        # 不要忘了装饰器参数和函数参数!
        def wrapped(*args, **kwargs):
            print("装饰函数得到的参数: {}, {}, {}, {}".format(decorator_arg1, decorator_arg2,
                                                     args, kwargs))
            return func(*args, **kwargs)

        return wrapped

    return my_decorator


@decorator_maker_with_arguments("a", "b")
# @no_argument_decorator
def no_argument_function():
    print("I am a 普通的无参数函数")


@decorator_maker_with_arguments("a", "b")
# @no_argument_decorator
def argument_function(*args):
    print("I am a 带多个参数的函数")


@decorator_maker_with_arguments("a", "b")
# @no_argument_decorator
def args_kwargs_function(*args, **kwargs):
    print("I am a 带 args & kwargs 参数的函数")


if __name__ == "__main__":
    no_argument_function()
    argument_function("c", "d")
    args_kwargs_function("c", "d")
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://l1nwatch.gitbook.io/interview_exercise/python-yu-yan-te-xing/6-mian-xiang-qie-mian-bian-cheng-aop-he-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.
