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 Methods

Hai Phan
Hai Phan
2,442 Points

What is the difference between kenneth.pickpocket() and Thief.pickpocket(arg) exactly?

I still don't know why we need to pass an argument to the method when we call it directly. Can someone explain, please?

2 Answers

Steven Parker
Steven Parker
231,248 Points

The first example is the proper way to call an instance method. The second example was only given to illustrate how "self" is used by the method, but it's not the proper syntax and should not be used in actual code.

Hai Phan
Hai Phan
2,442 Points

Thank you for the answer! Can you explain more about self argument. Kenneth said it pass an instance object to the method, so kenneth.pickpocket() = kenneth.pickpocket(kenneth)? What is this really mean? Why do we need self argument? I'm so confuse with OOP right now.

Steven Parker
Steven Parker
231,248 Points

You don't actually pass the instance named to the method. But the method is defined to take the instance as the first argument as if you do. But the actual value is supplied by the system and not passed by the programmer.

The "self" value is useful for accessing instance variables. Like in this case, the class includes a variable named "sneaky". So within the method, you can access it by using "self.sneaky".

Some of these concepts may not make sense at first, but I suggest that if you forge ahead some time and extra examples may help make them "sink in".

Hai Phan
Hai Phan
2,442 Points

Thank you so much, it helps me a lot :smiley:

Enrica Fedeli-Jaques
Enrica Fedeli-Jaques
6,773 Points

Hi Steven, in the video Kenneth says that if you call a method directly from the class then you need to provide the instance that is going to use the method, but from your comment above that's not a way to do it? thanks a lot!

Enrica Fedeli-Jaques
Enrica Fedeli-Jaques
6,773 Points

not an answer to the question, but I can relate to your "I'm so confused with OOP so far". what really helped me is going through the full course more than once and trying the tests until I got it right. it's frustrating until it "happens" but now I get what he's saying. I guess this bit of the course is quite a step up from the rest. :)

Steven Parker
Steven Parker
231,248 Points

You're right about the argument difference, but calling a method directly from the class is not typically done. Later on you'll learn about methods that are intended to be used directly. Those are called class methods to distinguish them from instance methods, and they are created a bit differently.