【发布时间】:2016-03-09 15:15:54
【问题描述】:
我正在创建一个类来进行一些计算。该课程将有 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
当我调用类时,我必须提供参数 --> 但是,正如你所看到的,我现在只处理第一个参数的值。第二个和第三个在整个班级中计算。当我只用一个参数调用类时,我当然会遇到参数失败的错误。有人对此有想法吗?
【问题讨论】:
-
那为什么还要传入这些参数呢?
-
如果在初始化时不知道这些属性的值,为什么要在初始化时提供这些属性?
-
如何让它们成为可选的或者只是初始化
self.cat_score_list = None
标签: python python-2.7 class