The function of the @ modifier in Python.

Source: Internet
Author: User

The ' @ ' symbol used as a function modifier is a new addition to python2.4, and the modifier must appear on the previous line of the function definition and is not allowed on the same line as the function definition. That is to say @a def f (): is illegal. Functions can be decorated only within a module or class definition layer, and no modification of a class is allowed. A modifier is a function that makes the modified function a parameter and returns a modified function with the same name or other callable object.

For example:

    def test02 (a):     Print ' test02 '     @test02    def  Test (A, b       ):print'test  '       print a+b

Results:

There are many explanations in the official Python documentation, but the code is very clear.

' @dec2 @dec1 def func (arg1, Arg2, ...):     Pass is   equivalent to:def  func (arg1, Arg2, ...):    Pass  = dec2 (DEC1 (func))

The Python @property property allows you to access the defined function as a property, providing a more friendly way to access it.

The function of the @ modifier 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.