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 Method Arguments

Jorgen Rasmussen
Jorgen Rasmussen
3,367 Points

Code challenge previous to this lesson:

This class should look familiar!

First, I need you to add a method named praise to the Student class. It should take the self argument. Then, inside the praise method, return a positive message about the student using the name attribute.

As an example, it could say "You're doing a great job, Jacinta!" or "I really like your hair today, Michael!".

Feel free to change the name attribute to your own name, too!

I write the following: class Student: name = "Your Name"

def praise(self):
    return "You rock,{}!".format(name)

And I get an error message saying: Bummer: NameError: name 'name' is not defined

but I defined name = Your Name

What am I missing?

im assuming that name is an attribute in your class, if so, you need to call it as self.name instead of name so your code should look something like:

class Student:
    name = "Your Name"

    def praise(self):
        return "You rock,{}!".format(self.name)

2 Answers

Jorgen Rasmussen
Jorgen Rasmussen
3,367 Points

Hello Jesus, That did it, thanks! So I had to put self because that is the name of the instance that will be created which the attribute name belongs to? Thanks again

yep, every time you create a class, all "variables" created directly in the class will be attributes, while you are declaring in the root of the class, in this case name, you write it as a variable, and in every method you call it or reference it as self.<attribute name>. Is not explicitly the "name" of the instance on the class created, that will be the variable were you assign the instance to, is more the reference of that instance to access the attributes and methods inside.

Jorgen Rasmussen
Jorgen Rasmussen
3,367 Points

Thanks again Jesus, your description helps me understand what is going on here Take Care :)