FAQ: Learn Python: Classes - Methods

This community-built FAQ covers the “Methods” exercise from the lesson “Learn Python: Classes”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

FAQs on the exercise Methods

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

6 posts were split to a new topic: Why do I get a SyntaxError when defining my class?

2 posts were split to a new topic: What does self do in the example? And is pipi_pitbull an object?

8 posts were split to a new topic: What do parenthesis do when calling classes and methods?

5 posts were split to a new topic: Do I need to call a class everytime to use its functions?

So just a little bit of background, I learnt OOPs and classes, constructors, destructors, and all in C++ before and now I’m doing Python 3 from your website.

Anyway, while defining member methods in classes in C++, I remember that the argument of a method never was the object that called that class. The arguments could be anything and were entered after instantiating an object of a class.

I’ll just put a snippet to make you understand my doubt better

//This is code written in C++

#include <iostream>
using namespace std;
 class Doubt
 {
     private:
     int a, b;
     
     public:
     void methoda(int a)
     {
        cout<<"This returns "<<a<<endl;
     }
     void methodsum (int a, int b)
     {
        cout<<"This returns "<<a + b;
     }
 };
 int main()
 {
    cout<<"Enter 2 nos. to be added ";
    int a1, b1;
    cin>>a1>>b1;
    Doubt d1;
    d1.methoda(5);
    d1.methodsum(a1, b1);
 }
//Code Ends

//Output
Enter 2 nos. to be added 2 1                                                                                                                                
This returns 5                                                                                                                                              
This returns 3
//Output ends here.

So as you can see, the first argument doesn’t have to be the object calling the member method in C++

My question is, does the first argument always have to be the object calling the member method of a class in Python?
Thank you.

That a method has access to the object the method is called on is the whole point of methods, c++ and python don’t differ there, both of them give the called method access to the object it is called on.

1 Like

But in C++ a method can have an empty argument list, while in Python it always has atleast one which is generally named self, right?

They both receive the object, so that’s not a difference. You’re asking if they’re different but describe something that is the same

You could say there’s a cosmetic difference I guess but not sure that’s very meaningful?

1 Like

Ah it makes sense, so even though they look different which they would because they are two different programming languages, but they do the same thing.
Thank you!

why do I have to use the parameter self in the method washing_brushes() and I don’t use it in the body of the method?

Edit: Please see @ionatan’s comment below for an accurate answer-
Standard methods (ignoring the built-in decorator methods) require self to be passed regardless of whether it’s used. It’s just part of the language. As for why this is the case there’s a lot of discussion you can look into if you wish, e.g.

As for why the instance objects reference is passed as the first argument and not automatic there’s a lot of discussion you can look into if you wish, e.g.

1 Like

I wouldn’t call it mandatory. If a method of an object gets called, the method receives that object as the first argument. This is for example entirely valid:

class stuff:
    def method():
        print('hi')

stuff.method()

(and if self isn’t used then it’s arguably not a method, that’s… a function.)

and then if one happens to want to make that available from an object:

class stuff:
    @staticmethod
    def method():
        print('hi')

stuff().method()

in the end it’s just a matter of passing in the same amount of arguments as there are parameters, but that’s up to the programmer

2 Likes

I had to go and look up the definition of method just to sort that out in my head but you’re quite right, will edit response to warn anyone viewing it in future.

I guess you can go either way with that.
Actually, I prefer to make sense of it as that a request is made to the object, and the object does whatever it does with that request, a method being a handler for such a request regardless of whether the object itself is involved in handling it… but then you might question why that object should be handling that request.

Without thinking of it as a request, it just becomes functions with a different syntax for calling, which is silly and pointless.

Mandatoryness … python is so pliable nothing is mandatory. Self doesn’t “have to be there”, but if it is being sent then it better be received too, even if it’s ignored.

1 Like