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 trialPhu Tran
Python Web Development Techdegree Student 4,687 PointsWhy it needs to be self.name instead of name(self) or name.self?
Hi,
Here is my code:
class Student: name = "Your Name"
def praise(self):
return "You inspire me, {}".format(self.name)
but I don't understand why we have to call self.name? I thought self is often after the "."
Do you have any resources or documents to clarify this concept? I am a non-native-speaker so I am really sorry if my question is unclear.
class Student:
name = "Your Name"
2 Answers
Stuart McIntosh
Python Web Development Techdegree Graduate 22,874 PointsHi there - not a reference, however, hopefully, this is might shed some light...
>>> class Student:
... name = 'Stuart'
...
>>> s1 = Student()
>>> s1.name
'Stuart'
As can be seen above we create a class called Student then instantiate a new instance s1 to access the name attribute we call s1.name Self relates to the object instance calling the praise method. If we took the example above the object would s1 so self would represent s1. If I am not making alot of sense check this URL out https://www.digitalocean.com/community/tutorials/how-to-construct-classes-and-define-objects-in-python-3
Unsubscribed User
17,003 PointsTreehouse uses Markdown to allow users to Style their text
Above they used a code block which you can find out more about here, or click here for the treehouse markdown course.
Phu Tran
Python Web Development Techdegree Student 4,687 PointsPhu Tran
Python Web Development Techdegree Student 4,687 PointsThank you, I think I understand it right now :) does it is like we let the function praise inherit the attribute name from class Student, right?
Chester Stephen
413 PointsChester Stephen
413 PointsJust a random question and totally unrelated. How do you add screenshots like what you guys did on the above posts?
I have a lot of questions and wanted to post my work to make more sense in my question but unable to do so.
Thanks in advance!
Stuart McIntosh
Python Web Development Techdegree Graduate 22,874 PointsStuart McIntosh
Python Web Development Techdegree Graduate 22,874 PointsHi there Phu, the method praise has self as a argument. Self is the object instance - so in the example you are passing in the s1 object. Does that make sense?
Phu Tran
Python Web Development Techdegree Student 4,687 PointsPhu Tran
Python Web Development Techdegree Student 4,687 PointsYes, after few practices I understand how it works now. Thank you!!!