Python Object-Oriented Python Instant Objects Your first method

Alice Giandjian
Alice Giandjian
3,604 Points

Object-Oriented Python First Class

I'm not sure why this isn't working? What am I doing wrong?

first_class.py
class Student:
    name = "Alice"

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

me = Student() 
praise(me)

1 Answer

Thomas Harris
Thomas Harris
6,820 Points

Hi Alice!

When you instantiate the class with me = Student() you then use the 'me' instance (variable) to call the functions in the class. Also, this snippet does not print anything; you could do that a couple of different ways, but this should do the trick:

class Student:
    name = "Alice"

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

me = Student() 
print(me.praise())
Thomas Harris
Thomas Harris
6,820 Points

Oh and you need to format with your string with self.name to reference the 'name' attribute in the praise() function ;)

Alice Giandjian
Alice Giandjian
3,604 Points

thank you so much! that makes sense!