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__

Class Student, kwargs, create name attribute while creating instance

class Student:
    name = "Ann"

    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)

myname = Student(name='Amar')
myname.name

This code works fine. However what i understood is I have to create name attribute while creating instance. This is the case where i dont know what attribute i may need in advance.

my init was like this

def __init__ (self, **kwargs):
        for key, value in kwargs.items():
            setattr(self, key, value)

And i was adding name attribute while creating instance like this

myname = Student(name='Amar')
myname.name

When i run the above code it was giving me init positional argument error. Takes 1 given 2. Would like to know what I'm i doing wrong here. Thanks in Advance.

first_class.py
class Student:
    name = "Ann"

    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, **kwargs):
        for key, value in kwargs.items():
            setattr(self, key, value)

myname = Student(name='Amar')
myname.name

1 Answer

Mark Sebeck
MOD
Mark Sebeck
Treehouse Moderator 37,329 Points

Your code is correct Amar. But code challenges aren't setup to experiment like this. Run your code in Workspaces and it works fine. Thanks for asking me a challenging question and keep doing extra work like this. Helps so much with learning. I just added a print to test it in Workspaces.

class Student:
    name = "Ann"

    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, **kwargs):
        for key, value in kwargs.items():
            setattr(self, key, value)

myname = Student(name='Amar')
myname.name

print(myname.name)

Thanks Mark. Appreciate your help.