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__

Kyle Salisbury
seal-mask
.a{fill-rule:evenodd;}techdegree
Kyle Salisbury
Full Stack JavaScript Techdegree Student 16,347 Points

__init__ question

The question states: 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.

I'm stuck on how to do this. I tried googling how to override the init method and the sample code below was something close to what other people were saying in stackoverflow but obviously this is not correct.

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

you remove the super class its not necessary and after that line its self.name = name put the colon : after the items(): then you will get it write

8 Answers

Kent Åsvang
Kent Åsvang
18,822 Points

Okay, so you are pretty close, I have added some comments that I hope will help you understand what is going on:

""" You don't have to initiate name with a value, the argument alone is enough """
def __init__(self, name=None, **kwargs):
""" super() - calls methods on the parent class, and is not necessary in this challenge."""
        super(Student,self).__init__()
"""this line is fine, although I prefer dot-notation myself. 'self.name'
        self["name"]=name
""" This one is also correct, just remember to put the colon after 'items():'"""
        for key, value in kwargs.items()
            setattr(self,key,value)

Here is my solution :

def __init__ (self, name, **kwargs):
    self.name = name

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

Hope this was helpful.

Mercedes Aker
Mercedes Aker
6,544 Points

This I have also tried but it continues to say, "Try again!"

still its not working out

class Student: name = "Your Name"

def __init__(self, grade):
    if grade>50:
        return self.praise
    else:
        return self.reassuran

please help me on the int thing

CHO Ming TSENG
CHO Ming TSENG
14,219 Points

class Student: def init(self,name): self.name=name

Kent Åsvang
Kent Åsvang
18,822 Points

It's very typical to forget small stuff like that. I would recommend using pycharm, or a text-editor on the side while walking through the tracks here on treehouse. That way you can get used to the error-messages and to find bugs like this in your code

Michael Ma
Michael Ma
4,294 Points

class Student: def init(self, name): self.name = 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()

class Student: name = "Pierre" def init (self, name, **kwargs): 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()