Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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 设计一个decorator在文本文件中写入函数返回值_Python_Python 2.7_Python Decorators - Fatal编程技术网

Python 设计一个decorator在文本文件中写入函数返回值

Python 设计一个decorator在文本文件中写入函数返回值,python,python-2.7,python-decorators,Python,Python 2.7,Python Decorators,装饰师应该是这样的 @log_message def a_function_that_returns_a_string(): return "a string" @log_message def a_function_that_returns_a_strings_with_a_newline(s): return "{}\n".format(s) 我的实现是这样的,但是没有在文件中写入任何内容,我对装饰师是新手 def log_message(func):

装饰师应该是这样的

@log_message
def a_function_that_returns_a_string():
    return "a string"
@log_message
def a_function_that_returns_a_strings_with_a_newline(s):
    return "{}\n".format(s)
我的实现是这样的,但是没有在文件中写入任何内容,我对装饰师是新手

 def log_message(func):
          def wrapper(*args, **kwargs):
              result = func(a)
              with open('test.txt', 'w') as f:
                 for row in results:
                     f.write("%s\n" % str(row))
              return result
          return wrapper
你就在不远处:
result=func(a)
需要是
result=func(*args,**kwargs)

这会打印到屏幕上。写入文件几乎是一样的:

def log_message(func):

    def wrapper(*args, **kwargs):
        result = func(*args, **kwargs)
        print(result)
        return result

    return wrapper


@log_message
def a_function_that_returns_a_string():
    return "a string"

@log_message
def a_function_that_returns_a_strings_with_a_newline(s):
    return "{}\n".format(s)


a = a_function_that_returns_a_string()
b = a_function_that_returns_a_strings_with_a_newline('abc')
你就在不远处:
result=func(a)
需要是
result=func(*args,**kwargs)

这会打印到屏幕上。写入文件几乎是一样的:

def log_message(func):

    def wrapper(*args, **kwargs):
        result = func(*args, **kwargs)
        print(result)
        return result

    return wrapper


@log_message
def a_function_that_returns_a_string():
    return "a string"

@log_message
def a_function_that_returns_a_strings_with_a_newline(s):
    return "{}\n".format(s)


a = a_function_that_returns_a_string()
b = a_function_that_returns_a_strings_with_a_newline('abc')

看来你刚刚复制了你得到的作业,现在正在分配给我们…@Carnivore先生我不明白装饰者的逻辑你能告诉我哪里不对吗?记得在附加模式下打开文件。否则,以前的结果将丢失。(这是家庭作业吗?Python中有一个logger模块,专为完成您想要的任务而设计……)最后,results中的
行需要一个迭代器。你确定你的退货类型吗?试着看看你能如何改进。看来你刚刚复制了你得到的作业,现在正在分配给我们…@Carnivore先生我不明白装饰者的逻辑你能告诉我哪里不对吗?记得在附加模式下打开文件。否则,以前的结果将丢失。(这是家庭作业吗?Python中有一个logger模块,专为完成您想要的任务而设计……)最后,results中的
行需要一个迭代器。你确定你的退货类型吗?试着看看如何改进。我们没有在func中发送任何值列表或dict,我们只是发送一个简单的字符串,为什么我们在args或kwargs中捕获它
*args
捕获提供给包装器调用的所有参数(任何形式)<代码>**kwargs
捕获传递给包装器的所有关键字参数(例如key='abc形式)。您需要将确切的参数传递给func。否则参数会丢失。如果我错了,就不需要返回结果。您必须返回结果。否则,修饰后的函数将不会返回任何内容。我们不会在func中发送任何值列表或dict。我们会发送一个简单的字符串,说明为什么要在args或kwargs中捕获它。
*args
捕获提供给包装器调用的所有参数(任何形式)<代码>**kwargs
捕获传递给包装器的所有关键字参数(例如key='abc形式)。您需要将确切的参数传递给func。否则参数会丢失。如果我错了,就不需要返回结果。您必须返回结果。否则,您修饰的函数将不会返回任何内容。