我坚持使用创建以下对象的最佳方法:
我需要在创建对象时返回一个字典。我本可以只使用一个函数,但实际情况是,通过在创建对象时传递的参数执行某些操作。
这是这种情况:
class Example(object):
def __init__(self, param_a, param_b, param_c):
self.a = param_a
self.b = param_b
self.c = param_c
_tmp_1 = self._complex_function_1()
_tmp_2 = self._complex_function_2()
_tmp_3 = self._complex_function_3()
# I need here to return a dictionary. Now I'm saving the
# result in a variable call "self.values" and then access to
# it from the object created, but I don't like this approach...
def _complex_function_1(self):
# Make some calculations using "self"
def _complex_function_2(self):
# Make some calculations using "self"
def _complex_function_3(self):
# Make some calculations using "self"
当我使用它时,我必须执行以下操作:
e = Example(a, b, c)
dict_values = e.values
简而言之:我想要的是Example返回字典。我认为将其作为单独的函数来做并不方便,因为这些方法专门用于Example将它们转换为函数,并且还因为它们使用了 self 的参数。
有什么建议么?