Python: Classes: Methods

  1. Is is correct to say that difference between a function and method is that method has mandatory argument that is used to call it while a program function does not need such an argument?

  2. If we had to list all differences between the two , what would they be?

  3. The role of argument self for a method - I do not understand completely - why is it mandatory - how does it help a method or the class or to say, python programming? Why has it been made necessary? What if the language was made so that this argument wasn’t compulsory - would that have resulted in some issues?
    This last question is to understand the python logic deeper.

Thank you.

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

that is going to be a very long answer. Lets try a short version

a method is part of a class, for example a string is class, and .lower() is a method of string class. so every time you make a new string:

"hello world"

you create a new instance of the string class

so methods work very different from functions. Here you can read some more:

https://data-flair.training/blogs/python-method-and-function/

this would allow us to make our own data type, for example a MultiDict class (a class which supports having the same keys multiple times), you can then implement the methods to specify the behavior this class

self refers to the current instance of the class, which i always like to demonstrate the following way:

class Example:
    def some_method(self):
        print('hello world');

example = Example()
example.some_method()
Example.some_method(example)

the second time i call the method, i call the method on the class, and pass the instance for the self parameter. When a method is called on a class instance, python is handling the self binding.

Depends on the language implementation, there are indeed languages (JavaScript) comes to mind, where keep tracking of the class instances is completely done by the language, no need for the self (this in JS) parameter

there are plenty of people of who argue that the self parameter is redundant and to explicit.

1 Like

So does that mean a class object is already prepared or assigned with self. If I have multiple methods in a class, will they all need to have self .

Can i have few more coding examples please - those will be really helpful.

Thank you

no, self is just there to represent the instance, for example:

class Example:
    def __init__(self, name):
        self.name = name

    def print_name(self):
        print('hello {}'.format(self.name))

abcdef = Example('abcdef')
abcdef.print_name()
Example.print_name(abcdef)
ghijkl = Example('ghijkl')
ghijkl.print_name()
Example.print_name(ghijkl)

the right names are printed because self keeps track of on which instance the method is called.

well, you have methods, but you also have class methods. Although you could also give these a self parameter, they are often given a cls parameter

the self is just naming convention, you could give it another name, although that would be frowned upon by your colleagues.

2 Likes

Soooo, self is assigned output of the object example - which is actually carrying the output of the class Example - did i get that right?

functions and methods do share the concept of parameters (at function/method declaration, so self is a parameter) and arguments (values for the parameters when calling function/method)

so self is parameter, example the argument.

3 Likes

brilliant… I think i am getting there. Thank you