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