I don’t know if you read this article have actual interview experience. As an applicant who has experienced many interviews, wang has been asked about modifiers many times. Next, let’s take a look at some advanced knowledge of modifiers.
Why advanced knowledge? Because when you go to a search engine and search for “Python modifiers”, you will find the same basic 🤡 with no advanced content 🤡 and no experience sharing 🤡, I thought I would write a different correct 😏.
The core idea of a modifier is to put a shell around the target object. The input of the target object passes through the modifier, and the output of the target object passes through the modifier.
An 🐧 group is prepared and can be added to 537131912
How are modifiers implemented inside Python
The source code:
def decorator(func) :
print('> > > > > > > > > > > > > > > > > > > >')
def inner() :
print('Embellished')
return func()
return inner
@decorator
def print_ok() :
print('ok')
print_ok()
Copy the code
The above code output is as follows:
>>>>>>>>>>>>>>>>>>>> is decorated with OKCopy the code
So what does the Python interpreter do to make this output possible? Print_ok = decorator(print_OK) print_OK = decorator(print_OK) If you print(type(print_OK)), you should see:
The source code:
def decorator(func) :
print('> > > > > > > > > > > > > > > > > > > >')
def inner() :
print('Embellished')
return func()
return inner
@decorator
def print_ok() :
print('ok')
print(print_ok)
Copy the code
Output from the above code:
>>>>>>>>>>>>>>>>>>>>
<function decorator.<locals>.inner at 0x000001DF9936AE50>
Copy the code
If you don’t use the modifier 👇
def print_ok() :
print('ok')
print(print_ok)
Copy the code
Output from the above code:
<function print_ok at 0x0000018599DAADC0>
Copy the code
You can see that print_OK is not print_OK by itself, but instead is the corresponding content of the return value of the decorator, which you think is still 👇
def print_ok() :
print('ok')
Copy the code
Print_ok actually points to 👇
def inner() :
print('Embellished')
return func()
Copy the code
We can also use the following code to implement a decorator that does not use the @ symbol.
The source code:
def decorator(func) :
print('> > > > > > > > > > > > > > > > > > > >')
def inner() :
print('Embellished')
return func()
return inner
def print_ok() :
print('ok')
print_ok = decorator(print_ok)
print_ok()
Copy the code
The above code output is as follows:
>>>>>>>>>>>>>>>>>>>> is decorated with OKCopy the code
When the @ operator is used in modifiers, the @ symbol is called syntactic sugar. What is syntactic sugar? You can do the same thing without it, but it’s better to write less code and make it prettier and more elegant. So we don’t have to use the @ symbol to use the decorator, we can do it with print_OK = decorator(print_OK).
When should I use a decorator
There are only two times to use modifiers:
- Passive source code
- Don’t want to touch the shit mountain
Summary is do not want to move the original, want to rewrite some new on the original basis.
Passive source code
Unactive source code is very well understood, need not explain it
We use a third library and we import it and we want to add something on top of it and we add modifiers, why do we add modifiers, because it’s not going to break it, it’s essentially a shell.
Don’t want to touch the shit mountain
Modifiers can be used when you look back at code from a few months ago and find that you don’t understand the logic of the code anymore, don’t want to change it, or don’t want to take the time to understand it. There are scenarios where you need to Review the code yourself), and modifiers act as patches.
The same applies to a colleague’s code 🤡.
What? You don’t know what shit mountain is ⛰️? See here 👉 Why is the ancestral code called “shit mountain”?
Built-in decorator
There are four built-in modifiers, respectively
-
classmethod
-
staticmethod
-
property
-
warps
Python has a lot of built-in modifiers, and the top four cover 99% of the usage of built-in modifiers.
So go to the search engines and search the above four keywords to learn more!
Custom decorators
There are four main cases:
-
A function modifier that takes no arguments
-
Function modifier with arguments
-
Class modifier with no arguments
-
Class modifier with arguments
It may seem complicated, but once you understand print_OK = decorator(print_OK), it’s pretty easy to understand the above four cases 😏
Above 4 kinds of circumstance how to write, how to use search engine to check! I gave you all the key words.