小编Pat*_*ick的帖子

如何缓存方法返回的值,这取决于其他类的属性?

简化代码(无缓存)

首先是一段简化的代码,我将用它来解释这个问题.

def integrate(self, function, range):
    # this is just a naive integration function to show that
    # function needs to be called many times
    sum = 0
    for x in range(range):
        sum += function(x) * 1
    return sum

class Engine:
    def __init__(self, capacity):
        self.capacity = capacity

class Chasis:
    def __init__(self, weigth):
        self.weight = weight

class Car:
    def __init__(self, engine, chassis):
        self.engine = engine
        self.chassis = chassis
    def average_acceleration(self):
        # !!! this calculations are actually very time consuming
        return self.engine.capacity …
Run Code Online (Sandbox Code Playgroud)

python oop

5
推荐指数
0
解决办法
549
查看次数

标签 统计

oop ×1

python ×1