我编写了以下代码来学习python中的闭包和装饰器
该代码在iPad上的Pythonista中执行良好
但是装修工不像我想象的那样工作。decorator旨在使函数在每次调用时以唯一的随机颜色打印出来。但是,看起来对函数的所有调用只调用一次装饰器。有人能解释一下原因吗
import random
import console
def random_color(func):
r = random.random()
g = random.random()
b = random.random()
print(f'console.set_color({r},{g},{b})')
console.set_color(r,g,b)
return func
@random_color # run set_tag function through decorator function.
def set_tag(tag):
def enclose_text(text):
print( f'<{tag}>{text}</{tag}>')
return enclose_text
# save enclose_text function with a remembered tag
h1 = set_tag('h1')
p = set_tag('p')
br = set_tag('br')
# execute enclose_text with different text strings
h1('Chapter One')
p('It was a dreary day. The rain had begun to set in ...')
br('')
h1('Chapter Two')
p('By the second day, the sun had returned to full strength.')
所有行的输出都是相同的颜色。下次我运行它时,所有的行都有相同的颜色——但颜色与第一次执行时不同。我希望装饰师使每个标签都有一个随机的颜色
有人能解释一下这不是什么情况吗
以下是输出:
<h1>Chapter One</h1>
<p>It was a dreary day. The rain had begun to set in ...</p>
<br></br>
<h1>Chapter Two</h1>
<p>By the second day, the sun had returned to full strength.</p>
装饰器在函数定义为时执行;decorator语法只是函数应用程序的语法糖
相当于
您应该这样定义装饰器:
也就是说,
random_color
应该返回一个设置控制台颜色的函数,然后调用原始函数此外,
set_tag
不是您想要修饰的函数:它是set_tag
创建的函数:以前,
set_tag
是一个函数,它会选择一种随机颜色,将控制台设置为使用该颜色,然后返回一个生成标记的函数。我假设对set_color
的调用此时会影响终端,而不是在print
最终被调用时。现在,它是一个函数,返回一个函数,该函数选择一个随机颜色,并使用该颜色生成一个标记相关问题 更多 >
编程相关推荐