As mentioned by several experts above, the function of decorator (@grammar) is:
@foo def bar(): ...
is equivalent to:
bar = foo(bar)
Translated into Chinese is:
Use the function name@的 function 當作引數來呼叫@function, 並且賦值給 被@
by
function
Because this action is very similar todecoration (modification, expansion, adjustment, restriction...)originalbar, so it is called decorator:
By definition, a decorator is a function that takes another function and extends the behavior of the latter function without explicitly modifying it.
Although it is said to be a decoration, the executedbar跟原本的bar早已不是同一個人了,foois no longer the same person as the original
.
foowill return a brand new object, usually a function, but it is very likely that the returned object is not connected at all. Function is neither:
def foo(func): return None @foo def bar(): ...
foo變成了一個很奇怪的裝飾器, 因為他返還的東西是
NoneIn the above example, , not only is there no decoration, it also destroys the function
Back to the example you gave at the beginning:
func_a, 你會發現錯誤被引發了, 因為func_aBut you can try calling
which is not a function at all. Of course, the effect you want to achieve cannot be reused
It is true that decorators do not have to use local functions or nested functions, but we usually let the decorator return a function. I think this is a very important point. After all, we all intuitively think that the decorated function is still a function. Function
There are thousands of decorators, and some can indeed use a single layer of nesting, such as registering functions:
The reason is that this action only needs to be processed once during decoration. You don’t want to register it every time you call the function. All we need is the original function.
But for actions likeprinting logorcalculating time
, etc., we still have to use nesting techniques, because we want to print and calculate every time we call the function. What we need is a new function, this Functions are created by local functions, which will inevitably lead to cascading and nesting. There are also some more complex decorators with parameters that may use more than two levels of nesting
Summary
Whether you want to nest or not depends on the purpose, but be sure to remember to return the function after decorating
Maybe you guys don’t understand what I mean. My idea is quite confusing. My question is why python is designed like this, because at first I thought it was useless to nest two layers of functions. A simple decorator and one layer of functions are enough to print a Log, time, etc., this is not the philosophy of Python. I thought about it for a night and understood it myself. The writing is a bit messy. Friends in need can take a look and marvel at the wonderfulness of this design pattern. The backward reasoning method used is to clarify the basic requirements and then reason backwards to understand why each step is written as such.
As mentioned by several experts above, the function of decorator (@grammar) is:
is equivalent to:
Translated into Chinese is:
functionBecause this action is very similar todecoration (modification, expansion, adjustment, restriction...)original
bar
, so it is called decorator:Although it is said to be a decoration, the executed
.bar
跟原本的bar
早已不是同一個人了,foo
is no longer the same person as the originalfoo
will return a brand new object, usually a function, but it is very likely that the returned object is not connected at all. Function is neither:foo
變成了一個很奇怪的裝飾器, 因為他返還的東西是None
In the above example, , not only is there no decoration, it also destroys the function Back to the example you gave at the beginning:foo
沒有return
述句, 這代表foo
會返還None
, 你寫的根本是個毀滅器(開玩笑), 你看到的效果只是曇花一現的假象, 那是因為在@
語法發揮作用的那一瞬間,print
Your syntax is executed
which is not a function at all. Of course, the effect you want to achieve cannot be reusedfunc_a
, 你會發現錯誤被引發了, 因為func_a
But you can try callingIt is true that decorators do not have to use local functions or nested functions, but we usually let the decorator return a function. I think this is a very important point. After all, we all intuitively think that the decorated function is still a function. Function There are thousands of decorators, and some can indeed use a single layer of nesting, such as registering functions: The reason is that this action only needs to be processed once during decoration. You don’t want to register it every time you call the function. All we need is the original function.
But for actions likeprinting logorcalculating time
, etc., we still have to use nesting techniques, because we want to print and calculate every time we call the function. What we need is a new function, this Functions are created by local functions, which will inevitably lead to cascading and nesting. There are also some more complex decorators with parameters that may use more than two levels of nesting Summary Whether you want to nest or not depends on the purpose, but be sure to remember to return the function after decoratingQuestions I answered
: Python-QAMaybe you guys don’t understand what I mean. My idea is quite confusing. My question is why python is designed like this, because at first I thought it was useless to nest two layers of functions. A simple decorator and one layer of functions are enough to print a Log, time, etc., this is not the philosophy of Python. I thought about it for a night and understood it myself. The writing is a bit messy. Friends in need can take a look and marvel at the wonderfulness of this design pattern. The backward reasoning method used is to clarify the basic requirements and then reason backwards to understand why each step is written as such.
Decorator happens in
定义
而不是执行
stage.Decorator function
outer
必须返回一个被装饰的函数
, 注意它必须返回一个定义
, 而不是调用
The detailed explanation downstairs is very good.
Many people must have shared this. I have also written a blog before. If the topic is interesting, you can check it out:
Detailed explanation of Python decorators
Equivalent to bar = foo(bar), just understand this
The second floor is right, the function is already executed when assigning a value to bar.