Python in the decoration, you can have parameters, return values, so how can this be both decorative decorator no argument there is no return value method, and the method returns a value or have parameters can decorate it? There is a universal decorator, as follows:
def decorate (test): # define a function decorator
def bold (* args, ** kwargs ): # * args may receive any number of parameters,
# ** args may receive any of a plurality of parameters dictionaries
print('style="font-weight:blod"')
res = test (* args, ** kwargs) # write, even if they are decorated test function has no
# Return value does not matter, does not complain.
return res #test function returns a value to return, no return None
return bold
The above code is completed python wording universal decorators, ( * args, ** kwargs ) may receive any number of parameters of any type, there can be no return values return . Modify the print sentence for their desired function, it is their a universal decorator. As a result, you can decorate the arbitrary function.
Use what function to test the above code:
@decorate # decorate a no argument method does not return value
def test1():
print ( "test1: no arguments, returns no value ")
@decorate # decorate a parameter of a method
def test2(name):
print ( "test2: parameters % s"% name)
@decorate # decorate a method returns a value of
def test3():
strs = "test3: with the return value "
return strs
test1 () # results: style = "font-weight: blod"
test1: no parameters, no return value
test2 ( "ss") # results: style = "font-weight: blod"
test2: parameters ss
a = Test3 ()
print (a) # results: style = "font-weight: blod"
test3: return value