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 trialnicolerogers3
11,451 PointsQuiz question: "Instance attributes always have to be passed in when an instance is created."
Question for clarification: the answer to this is falseβ is this just referring to a situation where there aren't any attributes defined on a specific instance? E.g. def __init__(self)
instead of def __init__(self, make, model, year)
?
1 Answer
Jennifer Nordell
Treehouse TeacherHi there, Nicole Rogers! Yes, you could have an instance of a class that takes no parameters and sets up a number of default attributes. You could also have set a default value directly in the parameters. So for instance, let's say we had a class Student
. We could set up the initializer to have a parameter of "programming_language". If we wanted to set a default value for, we'd do something like this:
def __init__(self, programming_language="Python"):
self.programming_language = programming_language
This would allow for two ways to create the instance. You could either do something like:
student1 = Student()
or
student2 = Student(programming_language="JavaScript")
Either one would work. The first one would automatically have their programming language preference set to Python and the second would have it set to JavaScript. So there are ways to create an instance without passing anything in at the time of creation and one of those is to have default values set in the parameters of the __init__
Hope this helps!
nicolerogers3
11,451 Pointsnicolerogers3
11,451 PointsThank you so much!