Skip to main content
 首页 » 编程设计

python之如何在不重复不必要的函数调用的情况下使用实用程序模块

2024年09月07日7insus

我有一个实用程序模块,用于向其他脚本提供数据。我想不出利用它的最佳方式,同时最大限度地减少函数调用的数量(为了论证,这些都是缓慢的)。

看起来像这样:

helper.py

dataset1 = slow_process1() 
dataset2 = slow_process2() 
 
def get_specific_data1(): 
    data = #do stuff with dataset1 
    return data 
 
def get_specific_data2(): 
    data = #do stuff with dataset1 
    return data 
 
def get_specific_data3(): 
    data = #do stuff with dataset2 
    return data 

现在,假设我需要在脚本中运行 get_specific_data1。在上面的设置中,我正在导入模块,这意味着我在导入时不必要地调用了 slow_process2。

如果我嵌套dataset1和dataset2的赋值,然后需要调用get_specific_data1和get_specific_data2同一个脚本,我运行 slow_process1 两次,这又是不必要的。

如果我创建一个带有get_specific_data 函数方法的Helper 类,它运行slow_process1 或slow_process2 如果需要,存储数据,然后在调用方法时可以根据需要访问我可以解决这个问题。这样合适吗?

类似于:

class Helper: 
    def __init__(self): 
        self.dataset1 = None 
        self.dataset2 = None 
 
    def run_dataset1(): 
        self.dataset1 = slow_process1() 
 
    def run_dataset2(): 
        self.dataset2 = slow_process2() 
 
    def get_specific_data1(): 
       if dataset1 is None: 
           self.rundataset1() 
       data = #do stuff with dataset1 
       return data 
 
    etc 

如果这是一个愚蠢的问题,我深表歉意,但我对 OOP 的经验有限,不想一开始就犯错误。

谢谢

请您参考如下方法:

这就是我所说的将类与 properties 一起使用的意思,只有在这种情况下,我使用了一个名为 lazyproperty 的自定义版本。它被认为是“惰性”的,因为它只在被访问时才被计算,就像一个普通的属性,但与它们不同的是,计算值以某种方式有效地缓存——将它变成一个实例属性——所以它不会每次都重新计算。

警告:这样做假设无论何时计算该值都是相同的,并且在第一次访问后对其所做的任何更改都将对同一实例的其他方法可见使用它的类——即他们不会看到新计算的值。

一旦完成,类中的方法就可以引用 self.dataset1 或 self.dataset2 就好像它们是常规实例属性一样,然后,如果它是第一次,将计算与其关联的数据,否则将简单地返回先前创建的值。您可以在生成的输出中看到这种情况(如下所示)。

# From the book "Python Cookbook" 3rd Edition. 
class lazyproperty: 
    def __init__(self, func): 
        self.func = func 
    def __get__(self, instance, cls): 
        if instance is None: 
            return self 
        else: 
            value = self.func(instance) 
            setattr(instance, self.func.__name__, value) 
            return value 
 
def slow_process1(): 
    print('slow_process1() running') 
    return 13 
 
def slow_process2(): 
    print('slow_process2() running') 
    return 21 
 
class Helper: 
    def __init__(self): 
        """ Does nothing - so not really needed. """ 
        pass 
 
    @lazyproperty 
    def dataset1(self): 
        return slow_process1() 
 
    @lazyproperty 
    def dataset2(self): 
        return slow_process2() 
 
    def process_data1(self): 
       print('self.dataset1:', self.dataset1)  # doing stuff with dataset1 
       return self.dataset1 * 2 
 
    def process_data2(self): 
       print('self.dataset2:', self.dataset2)  # doing stuff with dataset2 
       return self.dataset2 * 2 
 
    def process_data3(self): 
       print('self.dataset2:', self.dataset2)  # also does stuff with dataset2 
       return self.dataset2 * 3 
 
if __name__ == '__main__': 
 
    helper = Helper() 
    print(helper.process_data1())  # Will cause slow_process1() to be called 
    print(helper.process_data2())  # Will cause slow_process2() to be called 
    print(helper.process_data3())  # Won't call slow_process2() again 

输出:

slow_process1() running 
self.dataset1: 13 
26 
slow_process2() running 
self.dataset2: 21 
42 
self.dataset2: 21 
63