Learning Java all know that Java has faceted programming, that is, AOP, to put it bluntly is in your original code before the execution of some operations, such benefits is not to modify the original code logic, enhance the function of the program, very convenient. Life is short, I use Python, and python does the same thing, but it’s much more convenient, and that thing is a decorator
1. Parameterless decorators
Decoration without parameters is the simplest, everything is difficult at the beginning, first pick a simple start, don’t panic, take your time.
def aop(func): print("begin----------") func() print("end-----------") @aop def funcMethod(): Print (" run ") # # execution results begin -- -- -- -- -- -- -- -- -- -- # run # end -- -- -- -- -- -- -- -- -- -- -Copy the code
You can see that the syntax is to use @aop on the functions that need to be decorated. Aop represents the functions of the decorator.
2. Functions decorated with arguments
Parameterized functions are the norm in development, so how do you decorate parameterized constructors
Def def funcMethod(p1): def wrap(p1): print(p1) print(p1) return wrap @aop def funcMethod(p1): Print (p1) funcMethod(" execute ")Copy the code
You can see that this is a little more complicated than the first example, but you can also see the underlying principle of the decorator,
We want to modify a function with one argument, so we define a new function inside the decorator function with the same argument, and return the wrap function, replacing the funcMethod function
The decorator function replaces the original function as part of the new function.
3. Generic function decorator
Usually we want to implement a generic decorator, such as a function call log, so that each function is defined differently, some with many arguments, some with few arguments, and some with no arguments at all.
def aop(func): def wrapper(*args, **kwargs): Print (' : ') print(args) print(kwargs) result = func(*args, **kwargs) print(" end ") return result return wrapper @aop def funcMethod(p1,p2): Print (p1) print(p2) @aop def funcMethod(): print(" XXXXXX ") funcMethod(" execute "," parameter 2") funcMethod()Copy the code
Note: an args parameter is preceded by a ‘to indicate that multiple argument values can be stored in an array
Kwargs accepts the conversion of a parameter to a dictionary type by prefacing the parameter with”
Class-based decorators
class AopClazz(object):
def __init__(self, f):
self.f = f
def __call__(self):
print(" start")
self.f()
print(" end")
@AopClazz
def func():
print("func")
func()
Copy the code
Python has only one requirement for an object to be used as a decorator (@decorator) : The decorator must be a callable object.
The callable object we are most familiar with is the function.
In addition to functions, classes can also be Callable objects, as long as you implement the Call function (which the boxes above touched on), and less commonly used partial functions are also Callable objects.
5. Built-in decorator
Property decorator: @property
Classmethod decorator: @classmethod
Staticmethod decorator: @staticmethod
Import math class Circle: def __init__(self,radius): # radius self.radius=radius @property def area(self): Return math.pi*self.radius **2 # perimeter(self): return math.pi*self.radius # perimeter(selfCopy the code
The @property method can also access the area as if it were a data property, triggering a function that dynamically evaluates a value.
The other two are so familiar that I won’t go into them
@staticMethod returns a StaticMethod object, while @classMethod returns a ClassMethod object. They both call their own __init__() constructor.
Conclusion: Decorators are simply functions that replace the current implementation, without much secret
\