Above is pressing the button 1 on the deposit, otherwise the withdrawal.
No, deposit and withdrawal to enter the password Ah!!! So, we need to add a password to verify the code.
Incoming group: 125240963 to get dozens of sets of PDFs Oh!
As you can see, although the Password authentication function, but the code redundancy is relatively high, and now only simulates the withdrawal and deposit function, but also has the query function, transfer function and so on, then the redundancy is higher, and compared to the withdrawal and deposit function, the reusability is not so high, so we want to further optimize the code, Write the validation function inside the withdrawal and deposit functions.
Is there any way to extend the function of the original function without changing the original function and the invocation of the original function? Of course there is, this is the famous decorator in Python. Or use the code above to demonstrate
From now on, the red box above the code is no longer changed, kill not change, we have to implement the password verification of the deposit and withdrawal function.
Decorator decorated with parametric functions
Look at the code below.
Do you have any questions about this code?
Run results
The wrong reason to read the newspaper is that the inner () function has one more parameter. It is important to note that the return value of the adorner function is inner, which means that Myprint is now equivalent to inner.
Let's change the code.
To enable adorners to decorate more functions, we further refine the code.
Adorner decorated with return value function
It can be seen that, regardless of whether the decorated function has no return value, the result has no return value, because the reason is very simple, because the inner () function has no return value at all. In order to implement a function that has a return value, there is still a return value, which requires the inner function to be consistent with the return value of the decorated function.
As you can see, a function with a return value is decorated with a return value, and a function with no return value is decorated with no return value, which matches the result we want.
Double Grammar Sugar
In order to demonstrate the double grammar sugar more visually, I will not use the above example.
Let me define a function first.
Now I have a requirement that I want to print a line ' = ' and a line ' * ' on top of ' Python small White League ', that is, to achieve this effect.
So how do we write it? Everyone may think of the superposition of grammatical sugars, which of the following is the way of superposition?
Let's run it and we'll know.
As you can see, the overlay should be the first, that is to say, the outermost syntax of the sugar is performed first.
Syntactic sugars with parameters
To demonstrate the syntax sugar with parameters, I'm going to move one of the above pictures down.
Obviously, the red-box part of the code redundancy is relatively high, we can use the syntax of the parameter of the sugar to simplify
The decorator is here, I wish you a happy day!
This is the most comprehensive python decorator I've ever seen! I don't know how to say this!