我不确定我是否完全理解类/实例变量。我有这段代码:
class Question(object):
"""
Exercise parameters
"""
input_method = "MathKeyboard"
difficulty = 4
dirname = "../../../main/question_factory/int/generated_questions"
"""
Initialize the exercise
"""
def __init__(self, cur, new):
questionFileName = self.dirname + "/" + self.question_file()
self.questionNum = cur
with open(os.path.abspath(os.path.join(os.path.abspath(os.path.dirname(__file__)), questionFileName)), 'rb') as questionFile:
self.question = pickle.load(questionFile)
def question_file(self):
print self.questionNum <-
return "difficulty" + str(self.difficulty) + "_" + str(self.questionNum) + ".question"
并发出错误'Question' object has no attribute 'questionNum'
在标记的线上。你能帮我解决这个问题吗?提前谢谢。
答案 0 :(得分:0)
问题是,在初始化变量之前,请在question_file
中调用__init__
。首先设置变量。
class Question(object):
"""
Exercise parameters
"""
input_method = "MathKeyboard"
difficulty = 4
dirname = "../../../main/question_factory/int/generated_questions"
"""
Initialize the exercise
"""
def __init__(self, cur, new):
self.questionNum = cur
questionFileName = self.dirname + "/" + self.question_file()
with open(os.path.abspath(os.path.join(os.path.abspath(os.path.dirname(__file__)), questionFileName)), 'rb') as questionFile:
self.question = pickle.load(questionFile)
def question_file(self):
print self.questionNum
return "difficulty" + str(self.difficulty) + "_" + str(self.questionNum) + ".question"