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
Jacob Murphy
Full Stack JavaScript Techdegree Graduate 32,014 PointsBad 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:
- You only use two methods
- 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.
Jacob Murphy
Full Stack JavaScript Techdegree Graduate 32,014 PointsIt should do, it did at the time of posting, unless the challenge has been changed.
ilya A
3,802 Pointshow much of the code is necessary to pass the first task?
Jacob Murphy
Full Stack JavaScript Techdegree Graduate 32,014 PointsJust 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.
ilya A
3,802 Pointsthank you!
1 Answer
Jennifer Nordell
Treehouse TeacherHi 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!
Jacob Murphy
Full Stack JavaScript Techdegree Graduate 32,014 PointsI 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. :)
ilya A
3,802 Pointsilya A
3,802 PointsWhy doesn't your code pass in the challenge?