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 Object-Oriented Python Instant Objects Master Class

Maba Bah
Maba Bah
2,744 Points

Feel like I'm missing a small detail here

Can anyone tell me what I'm missing? Please explain your answer. Thank you!

racecar.py
class RaceCar:
    def __init__(self,color,fuel_remaining,**kwargs):
        self.color = color
        self.fuel_remaining = fuel_remaining
        self.kwargs = kwargs
        for key,value in kwargs.items():
            setattr(self,key,value)
    laps = 0       

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

1 Answer

You nearly got it. You just have to add the self keyword to the laps attribute in the init and run_lap method like you already did with color and fuel_remaining:

self.laps = 0

self.laps += 1

Otherwise your instance wont be able to access the laps attribute.

You also forgot to indent self.laps = 0. If you fix this it should work. If i explained something wrong please correct me.

Maba Bah
Maba Bah
2,744 Points

Thank you so much! One more question, is self.kwargs = kwargs necessary? I'm not too familiar with kwargs, I feel like it's wrong

You`re welcome.

You are right, self.kwargs = kwargs is not necessary. The for loop and setattr() will do the job. I just tested it out in the REPL, if we write self.kwargs = kwargs we could only use it to check which additional key value pairs we assigned to the instance by typing instance_name.kwargs.

So for example if we create the instance Mercedes = RaceCar("silver", 100, example1 = "example", example2 = 4) we could then type Mercedes.kwargs and would get the output {"example 1" : "example", "example2" : 4}.

I am not sure if the ability to check this could be useful but either way it is not necessary.