One, function modifier
' @ ' is used as a modifier for a function, which can be decorated in a module or in the definition layer of a class.
Occurs on the previous line of the function definition and does not allow the same row as the function definition
A modifier is a function that takes a modified function as an argument and returns a modified function with the same name or other callable thing?
Example:
1 defsquare_sum (FN):2 defSquare (*args):3 Print "1---", args4n =Args[0]5 #return N (n-1) * (2*n-1)/66 Print "2==", n (n-1) * (2*n-1)/67 PrintFn.__name__8FN (n (n-1) * (2*n-1)/6)9 Print "*"*15Ten returnFN (n (n-1) * (2*n-1)/6) One returnSquare A - - @square_sum the defSum_a (a): - Print "3=", a - -Sum_a (10)
View Code
Results
1---(,) 2== 285sum_a3= 285***************3= 285
As can be seen, he is the first to execute the Decorator function, and then have return FN, the modifier under the function is effective, otherwise the function sum_a is no result.
--------2016-7-19 17:21:15--
Source: @ symbol in "1" python
The @ in Python