Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Dates and Times in Python (2014) Let's Build a Timed Quiz App The Question Classes & the Plan

Philip Finch
Philip Finch
5,262 Points

What's the point of this Question class?

What is the purpose of the Question class? If we're just passing num1 and num2 values to Add and Multiply, what does inheriting the Question class do?

class Question:
    answer = None
    text = None


class Add(Question):
    def __init__(self, num1, num2):
        self.text = '{} + {}'.format(num1, num2)
        self.answer = num1 + num2


class Multiply(Question):
    def __init__(self, num1, num2):
        self.text = '{} * {}'.format(num1, num2)
        self.answer = num1 * num2

5 Answers

This question appears to be very old... but just in case anyone else reads this (I read all the questions and assume many others do too), I think I understand why the Question class is included, and I think this is a very important question. Let's imagine that as we continue to think of ways to improve our quiz program, it grows somewhat large. At this point, without the Question parent class, we would have numerous classes such as Add, Multiply, WhatIsTheAirSpeedVelocityOfAnUnladenSwallow, WhatIsYourFavoriteColor, and so on, which all share a commonality: they are instances of a question, which has some description of a problem and a correct answer, yet they are not actually organized together in any way. They are just "floating around" in the program, without any easy way to quickly identify them as instances of the abstract idea "Question". This would become seriously problematic as the program grows and developers need to make modifications and/or additions to the program as well as debug it. So even though the presence of the Question class does not technically change the behavior of the program as it stands now, its presence is absolutely essential for keeping the program from becoming a giant unsustainable mess as it grows larger and functionality is added. In some earlier videos, Kenneth mentioned the idea of refactoring. From Wikipedia: "Code refactoring is the process of restructuring existing computer code—changing the factoring—without changing its external behavior. Refactoring is intended to improve nonfunctional attributes of the software. " So we can think of what Kenneth has done here with the Question class as avoiding 'technical debt' by anticipating some of the refactoring he would need to do later. Think of it as cleaning your room a little each day rather than waiting for it to become a giant mess to deal with it. It's a good habit to get into because it makes your life a lot easier and saves a lot of time and headache in the long run.

More specifically, you're using the same attributes (answer, text) in both classes, without having to redefine them, since they are children of the Question class.

This isn't that necessary, but it is a good coding practice in this regard and Kenneth is trying to ensure we follow along with the OOP train of thought.

I would however like him to discuss getter and setter methods. It's something I've always struggled with (when do we just directly reference attributes vs when do we define a getter / setter method).

The Question Class is a parent to both (Add and Multiply), both will then be able to use the functions and parameters under Question Class. =D

Philip Finch
Philip Finch
5,262 Points

I guess it was just intended as a placeholder.

Yes, a place holder is also correct. Did I give the best answer? =D

Iulia Maria Lungu
Iulia Maria Lungu
16,935 Points

I would appreciate very much if Kenneth Love would give an answer to this question. I was wondering the same and I am not sure about the answers I read here. Thank you for your time!