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

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

How to calcutate several functions in class without reiteration

问题

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

  1. class Variables:
  2. def __init__(self, param1, param2):
  3. self._param1 = param1
  4. self._param2 = param2
  5. def var1(self):
  6. return self._param1 + 'foo'
  7. def var2(self):
  8. return var1() + 'bar'
  9. ....
  10. def var9(self):
  11. return var2() + var8()
  12. def var10(self):
  13. return var3() + var9()

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

  1. def var1(self):
  2. return self._param1 + 'foo'
  3. def var2(self):
  4. return self._var1 + 'bar'
  5. ....
  6. def var9(self):
  7. return self._var2 + self._var8
  8. def var10(self):
  9. return self._var3 + self._var9
  10. class Variables:
  11. def __init__(self, param1, param2):
  12. self._param1 = param1
  13. self._param2 = param2
  14. self._var1 = var1(self)
  15. self._var2 = var2(self)
  16. ....
  17. self._var9 = var9(self)
  18. self._var10 = var10(self)

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

英文:

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

  1. class Variables:
  2. def __init__(self, param1, param2):
  3. self._param1 = param1
  4. self._param2 = param2
  5. def var1(self):
  6. return self._param1 + 'foo'
  7. def var2(self):
  8. return var1() + 'bar'
  9. ....
  10. def var9(self):
  11. return var2() + var8()
  12. def var10(self):
  13. 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__:

  1. def var1(self):
  2. return self._param1 + 'foo'
  3. def var2(self):
  4. return self._var1 + 'bar'
  5. ....
  6. def var9(self):
  7. return self._var2 + self._var8
  8. def var10(self):
  9. return self._var3 + self._var9
  10. class Variables:
  11. def __init__(self, param1, param2):
  12. self._param1 = param1
  13. self._param2 = param2
  14. self._var1 = var1(self)
  15. self._var2 = var2(self)
  16. ....
  17. self._var9 = var9(self)
  18. 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

  1. class Variables:
  2. def __init__(self, param1, param2):
  3. self._param1 = param1
  4. self._param2 = param2
  5. self._var1 = self._generate_var1()
  6. ... 类似地处理 var2 var10
  7. def _generate_var1(self):
  8. var1 = self._param1 + 'foo'
  9. return var1
  10. def var1(self):
  11. return self._var1
  12. ... 类似地处理 var2 var10
英文:
  1. class Variables:
  2. def __init__(self, param1, param2):
  3. self._param1 = param1
  4. self._param2 = param2
  5. self._var1 = self._generate_var1()
  6. ... similarly for var2 through var10
  7. def _generate_var1(self):
  8. var1 = self._param1 + 'foo'
  9. return var1
  10. def var1(self):
  11. return self._var1
  12. ... 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:

确定