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 Your first method

Hai Phan
Hai Phan
2,442 Points

I don't know how to add an attribute to a method, can someone please help?

In challenge 1 in OOP, I try

class Student:
    name = "Hai"
    def praise(self):
        self = name    #This is where I don't know what to do
        return print("Good job, {}".format(self))

Now I got stuck and I don't have any idea about things interact inside Class!

2 Answers

Steven Parker
Steven Parker
216,083 Points

You don't need to add a new attribute, what you need is already stored in the class as "self.name".

And you only need to return the string directly, you do not need to "print" anything.

Hai Phan
Hai Phan
2,442 Points

I did it but the name attribute didn't store

class Student:
    name = "Hai"
    def praise(self):
        self.name
        return "Good job {}".format(self)
Steven Parker
Steven Parker
216,083 Points

It's already stored, you just need to reference it:

#       self.name                               # this isn't needed (and doesn't do anything)
        return "Good job {}".format(self.name)  # reference the stored name
Hai Phan
Hai Phan
2,442 Points

Thank you, Steve, now I got it.

horus93
horus93
4,333 Points

Yea Hai, you can even do

return "Good job {}".format(Student.name)

To get the same output I noticed fooling around in my IDE, but it won't pass the challenge unless you use self.name.

Steven Parker
Steven Parker
216,083 Points

You should always reference a non-static property using the instance name, not the class name.