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

Bad Class Exapmle

I was watching this talk by a core contributor to the Python language.

In it he says that you shouldn't use a class to define code if:

  1. You only use two methods
  2. One of those methods is __init__.

In the last code challenge in the first stage of the new Object-Oriented Python course, "Master Class", we code the following:

 class RaceCar():
    def __init__(self, color, fuel_remaining, laps, **kwargs):
        self.color = color
        self.fuel_remaining = fuel_remaining
        self.laps = laps

        for key, value in kwargs.items():
            setattr(self, key, value)

    def run_lap(self, length):
        self.fuel_remaining -= length * 0.125
        self.laps += 1

If we take the above talks advice (Which I think we probably should?) this is probably not the best way to write this class. Just thought I'd address this see what the general community think.

Why doesn't your code pass in the challenge?

It should do, it did at the time of posting, unless the challenge has been changed.

how much of the code is necessary to pass the first task?

Just checked for you, take away the run_lap function and any reference to laps in __init__ and it passes the first task, then add back as necessary.

thank you!

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! You are likely correct in that it might not need to be a class just based on what we have here. However, keep in mind that you're talking about a coding challenge which is designed to test a student's understanding of how to use a Class, its properties, initialize and instantiate the class. To quote this person he also quoted the "Zen of Python" stating "simple is better than complex". Given that same piece of advice directed towards the goal of the challenge, would it be better to have a challenge with an extra method that serves absolutely no additional purpose just for the sake of having 3 methods? That seems silly, in my honest opinion.

A challenge is designed to quickly assess your understanding of the material thus far. It is not meant to be a comprehensive project or even demonstration of one.

Just my two cents! :sparkles:

I agree, I mostly just wanted to let people know about the standard convention, but I love the feedback!

Treehouse has always, for me anyway, been a great place to learn best practices etc. so I just wanted to do my part in the discussion for what those practices are. :)