Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么不在每次调用装饰函数时执行装饰器呢?_Python_Decorator - Fatal编程技术网

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

Python 为什么不在每次调用装饰函数时执行装饰器呢?,python,decorator,Python,Decorator,我编写了以下代码来学习python中的闭包和装饰器 该代码在iPad上的Pythonista中执行良好 但是装修工不像我想象的那样工作。decorator旨在使函数在每次调用时以唯一的随机颜色打印出来。但是,看起来对函数的所有调用只调用一次装饰器。有人能解释一下原因吗 import random import console def random_color(func): r = random.random() g = random.random() b = random.ra

我编写了以下代码来学习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执行;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 
也就是说,random_color应该返回一个设置控制台颜色的函数,然后调用原始函数

此外,set_tag不是您想要装饰的功能:它是set_tag创建的功能:


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

包装器中的最后一行应该有return。如果我返回包装器中的最后一行,效果并不比我原来的代码好。每个set_标签的颜色都与以前完全相同。尽管我确实看到装饰师在每次通话中都会改变颜色,但这种颜色并不像我想象的那样作为自由变量使用。
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
def set_tag(tag):
    @random_color 
    def enclose_text(text):
        print( f'<{tag}>{text}</{tag}>')    
    return enclose_text