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 trialJekabs Dambergs
7,417 PointsI passed the test but didn't get what the hell I did..
I passed this test, by reading info in the teachers notes and community. However, I didn't quite get what the code does with these syntaxes. Like, I didn't understand what value do we add to this. If before defining the student's grading, whether they have good/bad grades- they receive a praise/reassurance, that's clear. But what did we do know and why using init and setattr for it?
As English isn't my native, maybe I'm just bad with wording or I totally didn't get this course. Can someone please explain in simplicity?
class Student:
name = "Your Name"
def praise(self):
return "You inspire me, {}".format(self.name)
def reassurance(self):
return "Chin up, {}. You'll get it next time!".format(self.name)
def feedback(self, grade):
if grade > 50:
return self.praise()
return self.reassurance()
def __init__(self, name, **kwargs):
self.name = name
for key, value in kwargs.items():
setattr(self, key, value)
2 Answers
Scott Bailey
13,190 PointsI'll give it a go but I'm still learning myself - hopefully you can take something from it, or someone else will come along and do a better job than I can!
def __init__(self, name, **kwargs):
self.name = name
for key, value in kwargs.items():
setattr(self, key, value)
self.name = name - This is the only required argument when calling the class, it's just so you have a name to use later
kwargs - Just incase they pass any other keyword arguments when they make an instance of the class, such as age=10, the for key, value in kwargs.items(), key will be "age" and the value is 10
setattr() will then asign deal with the "self" part. So setattr(self, key, value) would be setattr(self, age, 10) during the for loop you wrote. This would create self.age with a value of 10
You arn't using any in this exercise so if you don't understand them don't worry about it (you can always look that up another time). It's just there for practice in the exercise I think!
I hope this can help a bit, feel free to say if it hasn't!
Scott Bailey
13,190 PointsI completely agree with you about understanding it on paper. Itβs a constant process and the deeper you go the more you will understand it.
The sections Iβm working on now I get the idea of but I think Iβm a fair way of being able to use it in a real situation- but it will come!
Jekabs Dambergs
7,417 PointsJekabs Dambergs
7,417 PointsThanks Scott, much appreciate your effort in explaining!
Still, kind of get the stuff 'on paper' but if I would have to think of using it in real case, I probably wouldn't come up with it as it hasn't yet connected in my brain. Guess that's part of the learning process.