Python Object-Oriented Python Instant Objects Master Class

melisa magwizi
melisa magwizi
1,478 Points

At which point do I set the attributes for the instance

Kindly advise what is wrong with my code

racecar.py
class RaceCar:
    color ="Blue"
    fuel_remaining = "50"

    def __init__(self,color, fuel_remaining,**kwargs)
        self.color = color
        self.fuel_remaining = fuel_remaining

        for color, fuel_remaining in kwargs.items():
            setattr(self,color, fuel_remaining)

1 Answer

Megan Amendola
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree seal-36
Megan Amendola
Treehouse Teacher

Hi, Melisa!

class RaceCar:
    # these are class attributes
    # you don't need these
    color ="Blue"
    fuel_remaining = "50"

    def __init__(self,color, fuel_remaining,**kwargs)
        # these are instance attributes
        self.color = color
        self.fuel_remaining = fuel_remaining

        for color, fuel_remaining in kwargs.items():
            setattr(self,color, fuel_remaining)

You created 2 class attributes, which you don't need for this challenge. You only need to set 2 instance attributes whose values are passed in when the class is created. You already have the instance attributes set here:

    def __init__(self,color, fuel_remaining,**kwargs)
        # these are instance attributes
        self.color = color
        self.fuel_remaining = fuel_remaining

You set instance attributes inside of the __init__ method using self.attribute_name. For this example, the values will be passed in when an instance of the class is created like this: RaceCar('red', 50).

For setattr(), you can see how Kenneth does it in this video at 3:50. Remember, kwargs are like dictionary items. They have keys and values. When you create the for loop and set the items, you will need to use key and value instead of color and fuel_remaining. You've already set color and fuel_remaining, kwargs are other items that might be passed. For example: `RaceCar('red', 50, sponsor='Coke Cola', wins=5).

When you remove the class attributes and fix the setattr() your code should then pass. You've got this!