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__

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 28,992 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.