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 Object-Oriented Python Instant Objects __init__

HELP with challenge!!

Our Student class is coming along nicely!

I'd like to be able to set the name attribute at the same time that I create an instance. Can you add the code for doing that? Remember, you'll need to override the init method.

first_class.py
class Student:
    name = "Your Name"

      def __init__(self, name=None, **kwargs):
        super(Student,self).__init__()
        self["name"]=name
        for key, value in kwargs.items():
            setattr(self,key,value)

    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()

3 Answers

For anyone who needs help this worked for me

class Student:
    name = "Your Name"


    def __init__(self, name= "Your Name", **kwarg):
        self.name = name

        for key, value in kwarg.items():
            setattr(self, key, value)

    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()
AJ Salmon
AJ Salmon
5,675 Points

For task one, you won't need kwargs in your init. You just want to have name in there, so that when someone creates an instance of the class, the name attribute is set to whatever is passed to the class when it's created. You can use name=None, or simply name, in the parameter of init. It should result in this output:

>>> marcolamoon = Student('Marco')
>>> marcolamoon.name
'Marco'

(You can test this in workspaces)

Matt Fredericks
Matt Fredericks
5,399 Points

I think this is the only part which was required for task 1 of 2 def init(self, name= "Your name"): self.name = name