Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Object-Oriented Python Instant Objects __init__

Jekabs Dambergs
Jekabs Dambergs
7,417 Points

I 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?

first_class.py
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
Scott Bailey
13,177 Points

I'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!

Jekabs Dambergs
Jekabs Dambergs
7,417 Points

Thanks 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.

Scott Bailey
Scott Bailey
13,177 Points

I 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!