为什么不在每次调用装饰函数时执行装饰器呢?

2024-04-26 21:28:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我编写了以下代码来学习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>

Tags: thetotextbr颜色tagrandomh1
1条回答
网友
1楼 · 发布于 2024-04-26 21:28:38

装饰器在函数定义为时执行;decorator语法只是函数应用程序的语法糖

@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 

相当于

def set_tag(tag):  
  def enclose_text(text):
    print( f'<{tag}>{text}</{tag}>')    
  return enclose_text 

set_tag = random_color(set_tag)

您应该这样定义装饰器:

def random_color(func): 
    def wrapper(*args, **kwargs):
        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(*args, **kwargs)
  return wrapper

也就是说,random_color应该返回一个设置控制台颜色的函数,然后调用原始函数

此外,set_tag不是您想要修饰的函数:它是set_tag创建的函数:

def set_tag(tag):
    @random_color 
    def enclose_text(text):
        print( f'<{tag}>{text}</{tag}>')    
    return enclose_text 

以前,set_tag是一个函数,它会选择一种随机颜色,将控制台设置为使用该颜色,然后返回一个生成标记的函数。我假设对set_color的调用此时会影响终端,而不是在print最终被调用时。现在,它是一个函数,返回一个函数,该函数选择一个随机颜色,并使用该颜色生成一个标记

相关问题 更多 >