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!