我如何在Python中制作两个装饰器来完成以下操作?

@make_bold
@make_italic
def say():
   return "Hello"

调用say()应返回:

"<b><i>Hello</i></b>"

当前回答

考虑下面的修饰符,注意我们将wrapper()函数作为对象返回

def make_bold(func):
    def wrapper():
        return '<b>'+func()+'</b>'
    return wrapper

所以这个

@make_bold
def say():
    return "Hello"

计算结果为

x = make_bold(say)

注意,x不是say(),而是在内部调用say(()的包装器对象。这就是装饰师的工作原理。它总是返回调用实际函数的包装器对象。如果链接此

@make_italic
@make_bold
def say():
    return "Hello"

转换为此

x = make_bold(say)
y = make_italic(x)

以下是完整的代码

def make_italic(func):
    def wrapper():
        return '<i>'+func()+'</i>'
    return wrapper


def make_bold(func):
    def wrapper():
        return '<b>'+func()+'</b>'
    return wrapper


@make_italic
@make_bold
def say():
    return "Hello"


if __name__ == '__main__':
    # x = make_bold(say) When you wrap say with make_bold decorator
    # y = make_italic(x) When you also add make_italic as part of chaining
    # print(y())
    print(say())


上述代码将返回

<i><b>Hello</b></i>

希望这有帮助

其他回答

用于绘制图像的嵌套装饰器的又一示例:

import matplotlib.pylab as plt

def remove_axis(func):
    def inner(img, alpha):
        plt.axis('off')
        func(img, alpha)
    return inner

def plot_gray(func):
    def inner(img, alpha):
        plt.gray()
        func(img, alpha)
    return inner

@remove_axis
@plot_gray
def plot_image(img, alpha):
    plt.imshow(img, alpha=alpha)
    plt.show()

现在,让我们先使用嵌套的装饰器显示一个没有轴标签的彩色图像:

plot_image(plt.imread('lena_color.jpg'), 0.4)

接下来,让我们使用嵌套的装饰器remove_axis和plot_gray显示一个没有轴标签的灰度图像(我们需要cmap='gray',否则默认的颜色映射是viridis,因此除非明确指定,否则默认情况下灰度图像不会以黑白色显示)

plot_image(plt.imread('lena_bw.jpg'), 0.8)

上述函数调用缩减为以下嵌套调用

remove_axis(plot_gray(plot_image))(img, alpha)

说到计数器示例-如上所述,计数器将在使用decorator的所有函数之间共享:

def counter(func):
    def wrapped(*args, **kws):
        print 'Called #%i' % wrapped.count
        wrapped.count += 1
        return func(*args, **kws)
    wrapped.count = 0
    return wrapped

这样,您的装饰器可以重复用于不同的函数(或用于多次装饰同一个函数:func_counter1=counter(func);func_counter2=counter(func)),并且计数器变量将对每个变量保持私有。

这里是一个链接装饰器的简单示例。注意最后一行-它显示了封面下的情况。

############################################################
#
#    decorators
#
############################################################

def bold(fn):
    def decorate():
        # surround with bold tags before calling original function
        return "<b>" + fn() + "</b>"
    return decorate


def uk(fn):
    def decorate():
        # swap month and day
        fields = fn().split('/')
        date = fields[1] + "/" + fields[0] + "/" + fields[2]
        return date
    return decorate

import datetime
def getDate():
    now = datetime.datetime.now()
    return "%d/%d/%d" % (now.day, now.month, now.year)

@bold
def getBoldDate(): 
    return getDate()

@uk
def getUkDate():
    return getDate()

@bold
@uk
def getBoldUkDate():
    return getDate()


print getDate()
print getBoldDate()
print getUkDate()
print getBoldUkDate()
# what is happening under the covers
print bold(uk(getDate))()

输出如下所示:

17/6/2013
<b>17/6/2013</b>
6/17/2013
<b>6/17/2013</b>
<b>6/17/2013</b>

装饰只是语法上的糖。

This

@decorator
def func():
    ...

扩展到

def func():
    ...
func = decorator(func)

用不同数量的参数修饰函数:

def frame_tests(fn):
    def wrapper(*args):
        print "\nStart: %s" %(fn.__name__)
        fn(*args)
        print "End: %s\n" %(fn.__name__)
    return wrapper

@frame_tests
def test_fn1():
    print "This is only a test!"

@frame_tests
def test_fn2(s1):
    print "This is only a test! %s" %(s1)

@frame_tests
def test_fn3(s1, s2):
    print "This is only a test! %s %s" %(s1, s2)

if __name__ == "__main__":
    test_fn1()
    test_fn2('OK!')
    test_fn3('OK!', 'Just a test!')

结果:

Start: test_fn1  
This is only a test!  
End: test_fn1  
  
  
Start: test_fn2  
This is only a test! OK!  
End: test_fn2  
  
  
Start: test_fn3  
This is only a test! OK! Just a test!  
End: test_fn3