python @ modifier, this article is written clearly and white

Python @ modifier what the role is? Pants can be used to hide shame, but in the winter it can not protect us from the wind and cold. Smart people invented pants. After wearing trousers, the baby is no longer cold. Decorative device, as we are talking about here pants, underwear, without affecting the function of providing warmth to our bodies

Python is a function of the decorator essence, it allows other functions without changing the additional function without any added codes. Decorator return value is a function object. It is commonly used in the scene there is no related needs, as insertion logs, performance test, the transaction cache, permission checks and the like. Decorator is a great design to solve such problems. Use decorator, we can isolate a large number of the same code is not related to the function itself, and continue to reuse them. In short, the role of decorators is to add additional functionality to an existing object.

 

Here I give you write into that, in the Python function (function) occasionally (jing chang) will see the line of the function definition has modified @functionName, and when the interpreter read such a modifier of priority @ releasing the contents @, @ directly put the next row of the class as a parameter to a function or @ back function, and then returns the modified value to a function object.

To understand the role of the @ python decorator, the first thing to remember so few points:

1. decorative symbol "@" is a syntactic sugar, what does that mean? That said, I do not follow @ decorator syntax required to write, but in accordance with the general syntax requires python to write completely. So with @ decorator format to write the purpose is to write simple and convenient

2. What is the role of decorator is it? This simply means that: the original decorative function. What does that mean? For example, there is a function func (a, b), its function is to seek a, b of the difference, I now have a demand, just want to function under the decoration function again, and then take the absolute value of the difference between seeking complete, but can not func function within implemented, this time need a decorator function, such decorate = func (func) function, the function func transfer function as parameters to decorate, to decorate the function func rich, and then returned to complete the rich func, this when the function func on the rich.

3.python common sense, functions, and, like anything else, is an object. This means that the function may be passed as arguments to the function, or functions in the function as a return value returned. Example code verification:

Sample Code ### 
DEF function_1 (A): 
    Print ( "function_1") 
DEF function_2 (B): 
    Print (B (. 3)) 
    Print ( "function_2") 
 
 
@ function_1 
@ function_2 
DEF FUNCTION_NAME (n-): 
    Print ( "the Hello world, FUNCTION_NAME I AM ") 
    return. 5 n-+ 
... 
... 
#python be top-down order of the respective function result as input to the next function. 
# Output: 
 
"" " 
Hello World, I AM FUNCTION_NAME 
. 8 
function_2 
function_1 
" ""

  

My other articles:

python api call interface to obtain data, how to call api python interface (with code)

Why Python, Python is a high-level programming language

Do not Python good tangle which version in 2020 with Python3 on the right

Something small cat hair AI title automatically generated artifacts, do have a lot of artificial intelligence

Guess you like

Origin www.cnblogs.com/phploser/p/12650735.html