Member-only story
The Divine Library for Writing Decorators — Decorator
Today we are introducing a repository called decorator, which has been in existence for ten years but is still not popular, with a GitHub address https://github.com/micheles/decorator . What can this library do for you? It can help you write Python decorator code more conveniently. If you are not familiar with decorators, you can read previous articles first. More importantly, make the methods decorated by decorators in Python look more like the methods before decoration. This requirement, it has to be said, is indeed niche, but it can be quite troublesome to write it all from scratch when needed. Therefore, let’s take a look. The main content of this article is based on official documents. Those who are fluent in both English and Chinese can directly read the official documents
# Basic Definition
Before introducing the definition, add a basic concept:signature
In Python, each method has its own signature. According to PEP 362, the signature of a method contains all necessary information about the method, as well as corresponding parameter information. In Python 3, the signature also includes method comments to identify the type of parameters. After understanding the above definition, current decorators can be divided into two types:
- A signature invariant decorator, which is an executable object with both input and output methods, and the input and output method signatures are the same.
- A decorator that changes the signature of the…