为了账号安全,请及时绑定邮箱和手机立即绑定

如何有效地重新编码?

如何有效地重新编码?

慕姐8265434 2023-12-29 15:22:38
我有以下课程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
查看完整描述

4 回答

?
慕哥9229398

TA贡献1877条经验 获得超6个赞

由于我们只需要执行该函数一次,但我们不知道是否function_1首先执行function_2,因此可以使用以下想法:


compute如果可能,您可以在方法本身中调用该函数init。

def __init__(self, params):

    ....

    self._compute_start_end()

def _compute_start_end(self):

    ....

    self.start, self.end = start, end

def function_1(self):

    #use self.start and self.end

def function_2(self):

    #use self.start and self.end instead of recomputing

如果该声明由于某种原因不适用于您的程序,您可以使用简单的检查来检查该函数是否已被调用。

def __init__(self, params):

    ....

    self.start, self.end = None, None

def _compute_start_end(self):

    if (self.start or self.end):

        return

    ....

    self.start, self.end = start, end

def function_1(self):

    self._compute_start_end()

    #use self.start and self.end

def function_2(self):

    self._compute_start_end()

    #use self.start and self.end instead of recomputing

只要您不None同时分配start和end,计算就只会发生一次。


查看完整回答
反对 回复 2023-12-29
?
隔江千里

TA贡献1906条经验 获得超10个赞

好的。其他人这样做是作为答案而不是评论。我也把我的放在这里吧

我更喜欢

@functools.lru_cache(None)
def _compute_start_end(self):
     ....

并完成它。

所有其他解决方案都是正确的,但它们依赖于程序员记住在调用或_compute_start_end之前调用一次。告诉 Python 仅缓存结果使得此代码对未来的更改更具弹性。_function_1_function_2_compute_start_end



查看完整回答
反对 回复 2023-12-29
?
MM们

TA贡献1886条经验 获得超2个赞

如果您不想重新计算“开始,结束”,那么只需将它们作为实例变量即可


self.start = #Some Value

self.end = #Some Value

然后,当您在调用 function_1 之后调用 function_2 时,您可以执行类似的操作


    def _fuction_2(self):

        start, end = self.start, self.end


查看完整回答
反对 回复 2023-12-29
?
肥皂起泡泡

TA贡献1829条经验 获得超6个赞

有几种方法可以做到这一点。


在类中存储值

这可能是您可以做的最基本的事情:仅存储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_cache


class Problem:

    @lru_cache

    def _compute_start_end(self):

        ...

        return start, end

如果您使用 Python 3.9,您还可以使用functools.cache,它是lru_cache.



查看完整回答
反对 回复 2023-12-29
  • 4 回答
  • 0 关注
  • 62 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信