Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Object-Oriented Python Instant Objects Your first method

Everything should be working, but is not....

The code should be working but treehouse is telling that there's a mising 'self' in the praise method

first_class.py
class Student:
    name = "Nico"

    def praise(self):
        print ("You're doing a great job, {}!".format(self))


nick = Student()
nick.praise()

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! You're doing great, but there's a couple of things going on here. Unfortunately, the Bummer! message you're getting for this is a bit misleading. The first problem is that you're printing the string, but the challenge asks you to return the string. Also, you did include self as a parameter, but you're trying to format the string with self instead of the name property of self. Take a look:

    def praise(self):
        return "You're doing a great job, {}!".format(self.name)

Here we have the praise method which takes an argument of self. We return the string with a placeholder and format the placeholder with the value stored in the self.name property.

Also, the challenge does not ask you to create any instances of this class, nor call the method on this class. Treehouse is going to do that part for you. Hint :bulb: : Challenges can be very strict and it's a good idea to try not to do anything that isn't explicitly asked for. Even if functional, it can cause the challenge to fail.

Hope this helps! :sparkles:

Thank you!!! I'll be more carefull the instructions...

I don't understand why we have to use self.name and not just .format(name)

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi, Clifford Gagliardo! I also had a hard time wrapping my head around this at first. I'm going to show you a slightly more complex example, but I feel like it helps clarify what "self" actually does. This concept didn't start becoming more clear to me until I had examples that had multiple instances of the same object. I think you and I can agree that we are both students of Treehouse and we do share some characteristics, but we are both unique. Take a look at the example I made:

class Student:
  name = ""

  def __init__(self, name):
      self.name = name

  def praise(self):
        print ("You're doing a great job, {}!".format(self.name))

  def greet(self, other):
        print("Hi there, {}! My name is {}.".format(other.name, self.name))

me = Student("Jennifer")
you = Student("Clifford")

me.greet(you)  #I greet you
you.greet(me)  #You greet me back

In this example name is a property of the Student class. In fact, it's the only property at all. If this were a real world scenario we'd likely expect the student to have an ID (because there could be more than one John Smith, after all), and a lot of other generic information about the student.

In the lines where I define me and you, I'm creating an instance of that class. You are one student and I'm another. We have different names. I made a separate method that takes an instance of itself and an instance of another student. So we can say hello to each other! :smiley:

I feel like this might become more obvious when you see two separate instances of the same class used in conjunction.

Hope this helps! :sparkles:

The two lines printed will be:

Hi there, Clifford! My name is Jennifer. Hi there, Jennifer! My name is Clifford.