@ python in usage
@ Is a decorator for function calls pass parameters from the role.
There are modified and the modified difference, '@function' as a decorator, followed function used to modify (decorator may be another, the function may be defined).
Code 1
def funA(desA): print("It's funA") def funB(desB): print("It's funB") @funA def funC(): print("It's funC")
Results 1
It's funA
Analysis 1
@funA modification function definition def funC (), the FUNC () assigned to FUNA () of the parameters.
When executed from top to bottom, first define funA, funB, then run funA (funC ()).
At this time desA = funC (), then FUNA () Output 'It's funA'.
Code 2
def funA(desA): print("It's funA") def funB(desB): print("It's funB") @funB @funA def funC(): print("It's funC")
Results
It's funA
It's funB
Analysis
@funB modified decorator @ funA, @ funA modified function definition def funC (), the FUNC () assigned to FUNA () of the parameter, then funA (funC ()) assigned to funB ().
When executed from top to bottom, first define funA, funB, then run funB (funA (funC ()) ).
At this time desA = funC (), then FUNA () Output 'It's funA'; desB = funA (funC ()), then funB () Output 'It's funB'.
Code 3
def funA(desA): print("It's funA") print('---') print(desA) desA() print('---') def funB(desB): print("It's funB") @funB @funA def funC(): print("It's funC")
Results
It's funA
< function funC at 0x000001A5FF763C80 >
It's funC
It's funB
Analysis
Supra, in order to see more intuitive argument passing, desA printing, which is transmitted FUNC () address, i.e. desA now desA function ().
DesA performed () that is executed funC (), desA = desA ( ) = funC ().
Code 4
def funA(desA): print("It's funA") def funB(desB): print("It's funB") print('---') print(desB) @funB @funA def funC(): print("It's funC")
Results 4
It's funA
It's funB
None
Analysis
The above funC () as an argument to funA, then funA (funC ()) how to pass funB () do? Print desB, found no argument.
It can be understood as when the 'decorator' modified 'decorator', just call the function.
The above is a python small series to introduce the usage of @ Detailed integration, we hope to help