Python Decorator Initial Learning

Source: Internet
Author: User

The first step simple function

1 " " simple function: Call two times " " 2 def myfunc (): 3     Print ('myfunc () called. ' )45myfunc ()6 myfunc ()

The second step adorner provides additional functionality for the calling function

1 " "Replace function (decoration)2 The parameter of the adornment function is the decorated function object, returning the original function object3 The material statement of the ornament; myfunc = Deco (myfunc)" "4 defDeco (func):5     Print("before MyFunc () called.")6 func ()7     Print("After MyFunc () called.")8     returnfunc9 Ten defmyfunc (): One     Print("MyFunc () called.") A  -MyFunc =deco (MyFunc) - MyFunc () theMyFunc ()

The third step uses the syntax @ to decorate the function

1 " "use the syntax @ to decorate the function, equivalent to MyFunc = Deco (myfun)2 but the new function is called only the first time, and the original function calls once more" "3 defDeco (func):4     Print("before MyFunc () called.")5 func ()6     Print("After MyFunc () called.")7     returnfunc8 9 @decoTen defmyfunc (): One     Print("MyFunc () called.") A  - MyFunc () -MyFunc ()

Fourth Step

Python Decorator initial learning

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.