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 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
229,017 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
229,017 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
229,017 Points

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