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

Raduica Sebastian
PLUS
Raduica Sebastian
Courses Plus Student 1,356 Points

Can someone please explain me? Thank you

I don't really understand why i have to write self.name in the format method...why is this necessary? I would really apriciate an answer ! Thank you!

first_class.py
class Student:
    name = "Your Name"
    def praise(self):
        positive_message="Hello {} nice to meet you!!".format(self.name)
        return positive_message

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

Good question. The method praise is an instance method which means it will be part of an single instantiation of the Student class. The self signifies a reference to the current instance and the attributes, both class and instance attributes, it contains.

The parameter self Is automatically passed a reference to "this" instance to allow such references.

# create instance
my_instance = Student()  
# call method 
my_instance.praise()

In this case self.name would be set to my_instance.name

Post back if you need more help. Good luck!!!