如何有效地重新编码?

我有以下课程


class Problem:

    def __init__(self, instance: Instance):

        self.instance = instance

        self.solution =  Solution.empty_solution()


    def _compute_start_end(self):

        ....


        return start, end


    def _fuction_1(self):

        start, end = self._compute_start_end()

        ....


    def _function_2(self):

        start, end = self._compute_start_end()

        ....

对于该类型的每个对象,函数 1 和 2 将被调用一次且仅一次。但是,由于我在 中 计算了start和,所以我不想在调用 时重新计算它。如何避免这种情况?end_function_1_function_2


慕姐8265434
浏览 71回答 4
4回答

慕哥9229398

由于我们只需要执行该函数一次,但我们不知道是否function_1首先执行function_2,因此可以使用以下想法:compute如果可能,您可以在方法本身中调用该函数init。def __init__(self, params):    ....    self._compute_start_end()def _compute_start_end(self):    ....    self.start, self.end = start, enddef function_1(self):    #use self.start and self.enddef function_2(self):    #use self.start and self.end instead of recomputing如果该声明由于某种原因不适用于您的程序,您可以使用简单的检查来检查该函数是否已被调用。def __init__(self, params):    ....    self.start, self.end = None, Nonedef _compute_start_end(self):    if (self.start or self.end):        return    ....    self.start, self.end = start, enddef function_1(self):    self._compute_start_end()    #use self.start and self.enddef function_2(self):    self._compute_start_end()    #use self.start and self.end instead of recomputing只要您不None同时分配start和end,计算就只会发生一次。

隔江千里

好的。其他人这样做是作为答案而不是评论。我也把我的放在这里吧我更喜欢@functools.lru_cache(None) def _compute_start_end(self):      ....并完成它。所有其他解决方案都是正确的,但它们依赖于程序员记住在调用或_compute_start_end之前调用一次。告诉 Python 仅缓存结果使得此代码对未来的更改更具弹性。_function_1_function_2_compute_start_end

MM们

如果您不想重新计算“开始,结束”,那么只需将它们作为实例变量即可self.start = #Some Valueself.end = #Some Value然后,当您在调用 function_1 之后调用 function_2 时,您可以执行类似的操作    def _fuction_2(self):        start, end = self.start, self.end

肥皂起泡泡

有几种方法可以做到这一点。在类中存储值这可能是您可以做的最基本的事情:仅存储start并end放在类中。有时这称为“延迟初始化”。class Problem:    def __init__(self):        self._start, self._end = None, None    def _compute_start_end(self):        if not self._start and not self._end:            self._start, self._end = real_compute()        return self._start, self._end    def _function1(self):        start, end = self._compute_start_end()    def _function2(self):        start, end = self._compute_start_end()或者,您可以在构造函数中计算_startand _end。class Problem:    def __init__(self):        self._start, self._end = self._compute_start_end()    def _compute_start_end(self):        ...        return computed_start, computed_end    def _function1(self):        foo(self._start, self._end)    def _function2(self):        bar(self._start, self._end)记忆您还可以使用一种称为“memoization”的技术,该技术在 Python 中以functools.lru_cache. 这基本上会记住带有一组参数的函数调用结果,缓存并返回它而不是运行该函数。如果您不想让人造成员弄乱您的班级,那么这很好。from functools import lru_cacheclass Problem:    @lru_cache    def _compute_start_end(self):        ...        return start, end如果您使用 Python 3.9,您还可以使用functools.cache,它是lru_cache.
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python