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 Interactivity

Why can't i access to praise and reassurance methods?

OOP first_class.py I don't get how i'm supposed to call the method result.

first_class.py
class Student:
    name = "Your Name"
    def praise(self):
        return "You inspire me, {}".format(self.name)


    def reassurance(self):
        return "Chin up, {}. You'll get it next time!".format(self.name)


    def feedback(self, grade):
        if grade > 50:
            return self.praise()
        elif drage < 50:
            return self.reassurance()

2 Answers

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Alberto,

You are calling the praise and reassurance methods perfectly. Your problem is a simple typo in the elif line. You should be using grade but you have spelled it drage.

Cheers

Alex

Thanks, what a silly mistake.

Hrithik Sharma
Hrithik Sharma
6,090 Points

i have a stupid question why we need to call self.praise() why we can't just call praise() why this thing doesn't work

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Hrithik,

That's not a stupid question at all, indeed, it's a question that the designers of every object-oriented language have to ask themselves, and they don't all arrive at the same answer.

First, let's rewind a bit and consider why self is needed at all. self is the way we refer to the instance of an object from inside the object itself. If this is unfamiliar to you, I suggest taking a look at my answer to this question. The code is in Swift, not Python, but the issue that self addresses is the same.

It is typical (perhaps even universal) in OO languages for variables to have different levels of scope. One consequence of this is that you can have two variables with the same name that you want to access.

Let's create a simple example, based on the code from this challenge:

class Student:
    name = "Your Name"

    def praise(self):
        return "You inspire me, {}".format(self.name)

    def reassurance(self):
        return "Better luck next time!"

    def feedback(self, grade):

        def praise():
            return "That's pretty good, I guess"

        if grade > 50:
            return self.praise()
        elif grade > 40:
            return praise()
        else:
            return self.reassurance()

Note that in this example we have a praise defined at the object level, but we have another version defined inside the feedback method.

We need some way to know, when we are in feedback which version of praise we want to use. Python's solution to this problem is to insist, for consistency, that any time you want to use a name that is scoped to the object, you have to prefix it with self. This is true even if, as with our use of reassurance, there is no conflict with a local name.

In comparison, let's look at how Swift handles exactly the same class:

class Student {
    let name = "Your Name"

    func praise() -> String {
        return "You inspire me, \(name)"
    }

    func reassurance() -> String {
        return "Better luck next time!"
    }


    func feedback(grade: Int) -> String {
        func praise() -> String {
            return "That's pretty good, I guess"
        }

        if grade > 50 {
            return self.praise()
        } else if grade > 40 {
            return praise()
        } else {
            return reassurance()
        }
    }

}

Here, Swift's compiler knows that in feedback there are two possible uses of praise. So it requires that you use self when you want to use the object's version, and omit self when using the local version. But note that with reassurance, the compiler knows that there is only one version of it in scope, so lets you (but does not require you to) leave off the self when referring to the object's version of it. This would seem to be much more similar to how you would suggest implementing it.

So you see, two different approaches to the same problem.

Hope that helps,

Cheers

Alex

Hrithik Sharma
Hrithik Sharma
6,090 Points

Thank You so much you just cleared all of my doubt now i can move on in this course without any problem thank you again