From b9297cc001a4ea6948be96c39e0b0672982f5e2e Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Tue, 5 Feb 2013 11:05:15 -0500 Subject: [PATCH] question: Add Question._state_attributes for easier subclassing This way subclasses can extend the list instead of overriding __getstate__. --- quizzer/question.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/quizzer/question.py b/quizzer/question.py index 10ae4fd..f2e8ed0 100644 --- a/quizzer/question.py +++ b/quizzer/question.py @@ -6,6 +6,14 @@ def register_question(question_class): class Question (object): + _state_attributes = [ + 'id', + 'prompt', + 'answer', + 'help', + 'dependencies', + ] + def __init__(self, id=None, prompt=None, answer=None, help=None, dependencies=None): if id is None: @@ -26,13 +34,8 @@ class Question (object): type(self).__name__, self.id, id(self)) def __getstate__(self): - return { - 'id': self.id, - 'prompt': self.prompt, - 'answer': self.answer, - 'help': self.help, - 'dependencies': self.dependencies, - } + return {attr: getattr(self, attr) + for attr in self._state_attributes} def __setstate__(self, state): if 'id' not in state: -- 2.26.2