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 Basic Object-Oriented Python Emulating Built-ins __iter__

Using more than one default in Python Object parameters

When we open the workspace for this video, it has the following initalizer

 def __init__(self, model, year, make='Ford', gas=100):
...

def __str__(self):
    return f'{make} {model} {year}'

The instance is then defined as car_one = Car('Model T', 1908)

This returns the error

File  "/home/treehouse/workspace/cars.py", line 72, in <module> print(car_one)
File  "/home/treehouse/workspace/cars.py", line 19, in __str__ return f' {make} {model} {year}'
NameError: name 'make' is not defined

This makes it impossible to follow along in workspaces with the video.

And, while I'm on the topic, I have a related question. Yesterday I was trying out some code, and couldn't figure out how to use the first default and then change the second. For example, if I wanted the leave the make as Ford, but change the gas to 175. How does one do that?

Thank you!

Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

When I load the video linked here and create a new workspace, the __str__ method includes the correct self. prefixes. Is it possible you are using a workspace from a different video? Maybe this video needs a β€œuse new workspace” warning.

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

Hey amandae, the issue is the variable names aren’t referencing the instance attributes.

:point_right: each of them needs self. prepended to them:

return f”{self.make} {self.model} {self.year}”

To change a default parameter include a keyword argument for that parameter. So the call now becomes:

car_one = Car('Model T', 1908, gas=175)

Post back if you need more help. Good luck!!!

Thanks. That does fix the problem in the example code. I'll send an email about that. Now that you say it, the fix seems so simple!

For the second part of the question, let me re-ask. I wasn't clear. I'm not trying to change the default, but change it from a default in an instance.

car_one = Car('Model T', 1908, ,175) <-- This doesn't work, and neither does undefined. What needs to be in the "make" position in the instance to leave the default, but then change the next default?

Thank you!

Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

Apologies, I was also unclear. I meant that the gas=175 was to be used in the calling arguments. I’ve updated my answer above.

Got it! Thank you!