我正在创建一个类来进行一些计算。这个类有3个参数可以开始。我以简化的方式做了这样的事情:
class TheCalcs:
def __init__(self, pk_from_db, cat_score_list, final_score):
self.pk_from_db = pk_from_db
self.cat_score_list = cat_score_list
self.final_score = final_score
def calculate_cat_score(self):
#Do some calcs with the data of the pk_from_db and return that!
a_list_of_scores = [] # create a list of scores
return a_list_of_scores
def final_score(self): # The argument for this function would be the return of the calculate_cat_score function!
# Again do some calcs and return the final score
the_final_score = int()
return the_final_score
def score_grade(self): # the argument this this function again the return but now from the final_score function
# Do some cals and return the grade
the_grade = ("a string", "an integer")
return the_grade
当我打电话给课时,我必须提出论据 - >但是你可以看到我现在只做第一个参数的值。第二个和第三个是在整个班级计算的。当我用一个参数调用该类时,我当然会有一个错误的参数错误。有人对此有所了解吗?
答案 0 :(得分:0)
如果计算了这些值,就不要让它们成为参数。您可以改为调用这些计算方法来计算值:
class TheCalcs:
def __init__(self, pk_from_db):
self.pk_from_db = pk_from_db
self.cat_score_list = self.calculate_cat_score()
self.final_score = self.calculate_final_score()
# ...
或推迟计算,直到您需要它们为止。