简体   繁体   English

我何时应该将函数的结果存储为python中的变量?

[英]When should I store the result of a function as a variable in python?

Suppose a function my_list(obj) returns a list. 假设函数my_list(obj)返回一个列表。 I want to write a function that returns the single element of my_list(obj) if this list has length one and False otherwise. 我想编写一个函数,如果此列表的长度为1,则返回my_list(obj)的单个元素,否则返回False My code is 我的代码是

def my_test(obj):
    if len(my_list(obj)) == 1:
        return my_list(obj)[0]
    return False

It just dawned on me that the code 代码刚出现在我身上

def my_test(obj):
    L = my_list(obj)
    if len(L) == 1:
        return L[0]
    return False

might be more efficient since it only calls my_list() once. 可能会更有效,因为它只调用一次my_list() Is this true? 这是真的?

The function my_list() could possibly be computationally intensive so I'm curious if there is a difference between these two blocks of code. 函数my_list()可能需要大量计算,因此我很好奇这两个代码块之间是否存在差异。 I'd happily run a test myself but I'm not quite sure how to do so. 我很乐意自己进行测试,但我不确定如何进行测试。 I'm also curious if it is better practice in general to store the result of a function as a variable if the function is going to be called more than once. 我也很好奇,如果函数将被多次调用,通常最好的方法是将函数的结果存储为变量。

You are correct. 你是对的。 The second block would be more efficient since it only calls my_list() once. 由于第二个块仅调用一次my_list()因此效率更高。 If my_list() isn't particularly computationally expensive, it's unlikely you will notice a difference at all. 如果my_list()在计算上不是特别昂贵,那么您根本不会注意到两者之间的差异。 If you know it will be expensive, on the other hand, it is a good idea to save the result where you can if it does not hamper readability (however, note the caveat in @Checkmate's answer about memory for a possible exception). 另一方面,如果您知道这样做会很昂贵,则最好将结果保存在不影响可读性的位置(但是,请注意@Checkmate的答案中有关内存的警告, 以防可能出现的异常)。

However, if my_list() has side effects , or if it's return value may change between those two invocations, you may not want to save it (depends on if you want to trigger the side effects twice or need to receive the changed return value). 但是,如果my_list()副作用 ,或者在两次调用之间它的返回值可能发生变化,则您可能不想保存它(取决于您是想两次触发副作用还是需要接收更改后的返回值) 。

If you wanted to test this yourself, you could use time.time like this : 如果您想自己测试,可以使用time.time这样

import time

t0 = time.time()
my_test()
t1 = time.time()

total = t1-t0

to get the time for my_test() . 获得my_test()的时间。 Just run both functions and compare their time. 只需运行两个功能并比较它们的时间即可。

To answer your question on whether it is generally better to store the result of a function as a variable if it is going to be called more than once: it depends. 要回答您的问题,如果将函数的结果多次调用,通常最好将函数的结果存储为变量:这取决于。 In terms of readability, it's really up to you as a programmer. 就可读性而言,这实际上取决于您是一名程序员。 In terms of speed, storing the result in a variable is generally faster than running the function twice. 在速度方面,将结果存储在变量中通常比运行函数两次要快。

Storing the result can, however, uses memory, and if you're storing an unusually large variable, the memory usage can actually lead to longer running time than simply calling the function. 但是,存储结果可能会占用内存,并且如果您存储的是一个非常大的变量,与简单地调用函数相比,内存的使用实际上会导致更长的运行时间。 Further, as noted above, running a function can do more than just storing the result in a variable, so running a function a different number of times can give a different result. 此外,如上所述,运行一个函数可以做的不仅仅是将结果存储在变量中,所以运行一个函数不同的次数可以得到不同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM