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 trialHai Phan
2,442 PointsI 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
231,275 PointsYou 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.
horus93
4,333 PointsYea 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
231,275 PointsYou should always reference a non-static property using the instance name, not the class name.
Hai Phan
2,442 PointsHai Phan
2,442 PointsI did it but the name attribute didn't store
Steven Parker
231,275 PointsSteven Parker
231,275 PointsIt's already stored, you just need to reference it:
Hai Phan
2,442 PointsHai Phan
2,442 PointsThank you, Steve, now I got it.