Python Decorator Notes

Source: Internet
Author: User

First, the adorner has no parameters 1. The original function has no parameters
defWrap_in_tag_b (FN):#Wrap_in_tag_b is a real decorator.    defwrapped ():return "<b>"+ fn () +"</b>"    returnWrappeddefwrap_in_tag_i (FN):defwrapped ():return "<i>"+ fn () +"</i>"    returnwrapped@wrap_in_tag_b@wrap_in_tag_idefHello ():return "Hello"Print(Hello ())#returns "<b><i>hello world</i></b>"

2. Original function with parameters
defWrap_in_tag_b (FN):defWrapped (ARG):return "<b>"+ fn (ARG) +"</b>"    returnWrappeddefwrap_in_tag_i (FN):defWrapped (ARG):return "<i>"+ fn (ARG) +"</i>"    returnwrapped@wrap_in_tag_b@wrap_in_tag_idefHello (name):return "Hello {}". Formar (name)Print(Hello (' Jack '))

Second, the adorner with parameters 1. The original function has no parameters
defWrap_in_tag (deco_arg):#Adorner parameter: Deco_arg    #can be used in any location    defReal_decorator (func):#Real_decorator is the real decorator that is about to return.        defwrapped ():return "<{0}>{1}</{0}>". Format (Deco_arg, func ())returnWrappedreturnReal_decorator@wrap_in_tag ('b') @wrap_in_tag ('I')defHello ():return "Hello"    Print(Hello ())

2. Original function with parameters
defWrap_in_tag (deco_arg):#Adorner parameter: Deco_arg    #can be used in any location    defReal_decorator (func):#Real_decorator is the real decorator that is about to return.        defWrapped (func_arg):#original function parameter: Func_arg            #can only be used at this location            return "<{0}>{1}</{0}>". Format (Deco_arg, func (func_arg))returnWrappedreturnreal_decorator @wrap_in_tag ('b') @wrap_in_tag ('I')defHello (name):return "Hello {}". Format (name)Print(Hello ('Jack'))


Three, the adorner Class 1. No parameters for the original function
classWrap_in_tag (object):def __init__(self, deco_arg): Self.tag=Deco_argdef __call__(Self, func):defNewf ():return "<{0}>{1}</{0}>". Format (Self.tag, func ())returnNewf@wrap_in_tag ('b') @wrap_in_tag ('I')defHello ():return 'Hello' Print(Hello ())

2. Original function with parameters
classWrap_in_tag (object):def __init__(self, deco_arg): Self.tag=Deco_argdef __call__(Self, func):defNewf (func_arg):return "<{0}>{1}</{0}>". Format (Self.tag, func (func_arg))returnNewf@wrap_in_tag ('b') @wrap_in_tag ('I')defHello (name):return "Hello {}". Format (name)Print(Hello ('Jack'))

Iv. Decorating with decorators 1. Using functions as Adorners
defWrap_in_tag (deco_arg):#Adorner parameter: Deco_arg    #can be used in any location    defReal_decorator (func):#Real_decorator is the real decorator that is about to return.        defWrapped (self, func_arg):#The class method receives the first argument that is self, which must have, regardless of whether the FUNC_ARG has!!!            #original function parameter: Func_arg            #can only be used at this location            return "<{0}>{1}</{0}>". Format (Deco_arg, func (func_arg))returnWrappedreturnReal_decoratorclassFoo (object): @wrap_in_tag ('b') @wrap_in_tag ('I')    defHello (self, name):return 'Hello {} ". Format (name)@wrap_in_tag ('b') @wrap_in_tag ('I')    defByebye (self, name):return 'Byebye {} ". Format (name)F=foo ()Print(F.hello ('Jack')

2. Using classes as adorners
classWrap_in_tag (object):def __init__(self, deco_arg): Self.tag=Deco_argdef __call__(Self, func):defNewf (SLF, Func_arg):#SLF must! Self is occupied, then give the first parameter another name SLF (whatever)            return "<{0}>{1}</{0}>". Format (Self.tag, func (func_arg))returnNewfclassFoo (object): @wrap_in_tag ('b') @wrap_in_tag ('I')    defHello (self, name):return 'Hello {} ". Format (name)@wrap_in_tag ('b') @wrap_in_tag ('I')    defByebye (self, name):return 'Byebye {} ". Format (name)

Python Decorator Notes

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.