The @ in Python

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.