如何在类中计算多个函数而无需重复操作。

huangapple go评论67阅读模式
英文:

How to calcutate several functions in class without reiteration

问题

需要计算一些变量,这些变量可以被其他变量使用:

class Variables:
    def __init__(self, param1, param2):
        self._param1 = param1
        self._param2 = param2
    
    def var1(self):
        return self._param1 + 'foo'
    def var2(self):
        return var1() + 'bar'
    ....
    def var9(self):
        return var2() + var8()
    def var10(self):
        return var3() + var9()

问题是,在模块外部调用它们时,中间函数var1..var9多次计算。我尝试在__init__中计算它们:

def var1(self):
    return self._param1 + 'foo'
def var2(self):
    return self._var1 + 'bar'
....
def var9(self):
    return self._var2 + self._var8
def var10(self):
    return self._var3 + self._var9

class Variables:
    def __init__(self, param1, param2):
        self._param1 = param1
        self._param2 = param2
        
        self._var1 = var1(self)
        self._var2 = var2(self)
        ....
        self._var9 = var9(self)
        self._var10 = var10(self)

但是我的直觉告诉我,这个决定很糟糕:类内外的self是“不同的”。出于某些原因,我需要10个单独的函数,不能将它们合并为一个大函数。最佳方法是如何计算和存储它们?

英文:

I need to calculate some variables, which can be used by other variables:

class Variables:
    def __init__(self, param1, param2):
        self._param1 = param1
        self._param2 = param2
    
    def var1(self):
        return self._param1 + 'foo'
    def var2(self):
        return var1() + 'bar'
    ....
    def var9(self):
        return var2() + var8()
    def var10(self):
        return var3() + var9()

Problem is that intermediate functions var1..var9 are calculating multiple times when I call them outside module. I tried to calculate them in __init__:

def var1(self):
    return self._param1 + 'foo'
def var2(self):
    return self._var1 + 'bar'
....
def var9(self):
    return self._var2 + self._var8
def var10(self):
    return self._var3 + self._var9


    class Variables:
        def __init__(self, param1, param2):
            self._param1 = param1
            self._param2 = param2
            
            self._var1 = var1(self)
            self._var2 = var2(self)
            ....
            self._var9 = var9(self)
            self._var10 = var10(self)

But my intuition tells me that this decision is terrible: self-s inside and outside class are "different".
For some reasons I need 10 separate functions and cant combine them in one big function. What is the best way to calculate and store them?

答案1

得分: 1

class Variables:
    def __init__(self, param1, param2):
        self._param1 = param1
        self._param2 = param2

        self._var1 = self._generate_var1()
        ... 类似地处理 var2 到 var10

    def _generate_var1(self):
        var1 = self._param1 + 'foo'
        return var1

    def var1(self):
        return self._var1

    ... 类似地处理 var2 到 var10
英文:
class Variables:
    def __init__(self, param1, param2):
        self._param1 = param1
        self._param2 = param2

        self._var1 = self._generate_var1()
        ... similarly for var2 through var10

    def _generate_var1(self):
        var1 = self._param1 + 'foo'
        return var1

    def var1(self):
        return self._var1

    ... similarly for var2 through var10

huangapple
  • 本文由 发表于 2023年7月14日 06:00:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76683498.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定