Summary: In this tutorial, we will learn what methods are in Python and how it differs from a normal Python function.

Methods in Python are functions that are associated with the object of a class. They are always called on or with an object.

For example, append is a method because we need an object like a list to call and append the new item.

>>> l = [10, 20, 30, 40]
>>> l.append(50)
>>> print(l)
[10, 20, 30, 40, 50]

A method is always associated with an object and is invoked on the same.

Unlike function, which performs the task on arguments, a method performs tasks on the object of the class with which it is associated.

For instance, in the above example, the append method is adding a new item to the list object l.

Syntax to Invoke a Method

The general syntax to invoke any method is:

object.method_name()

In the statement to invoke a method in Python, the name of the method follows the object separated by a dot (.), followed by the call operator ().

Examples of Methods in Python

There are many methods that are built-in in Python and which work on different types of objects.

Examples of some of the built-in methods are list.append(), list.sort(), list.pop(), set.union(s), etc.

>>> l = [3, 1, 2, 4]
>>> l.sort()
>>> print(l)
[1, 2, 3, 4]

Like normal functions, the methods can also accept arguments to operate on the associated object.

One example of such type of method is set.union(s). This method combines the elements of the Python set passed as an argument (s) with the elements of the associated set object and returns them in the form of a set.

>>> s = {1, 5, 6}
>>> s.union({1, 2, 3})
{1, 2, 3, 5, 6}

Because Python is an OOP language, it allows creating our own methods by defining a class.

For example, in the following code, we have defined a user-defined class Square that contains a list called numbers (to store integers) and a method compute to compute and return the squares of all the numbers in the list.

class Square:
    def __init__(self, numbers: list):
        '''
        Parameters:
            numbers: List of integers
        '''
        self.numbers = numbers
        
    
    #example of an method
    def compute(self):
        '''
            Return the squares of the numbsers list.
        '''
        return [x*x for x in self.numbers]
        
s = Square([1, 2, 3, 4, 5])
print(s.compute())        #outputs [1, 4, 9, 16, 25]

How Methods differ from Functions?

Although methods are functions, since they are associated with objects, they operate differently than functions.

MethodsFunctions
Methods are associated with the objects.Functions are not associated with objects.
Methods can only be invoked on the associated objects.The execution of functions is not dependent on any object.
Methods make changes to the object.Functions use arguments to perform a specific task.
Methods are defined inside of a class.We don’t need a class to define a function.

Conclusion

Methods are functions in Python that are associated with the object of a class and can only be invoked on the associated object.

Methods in Python either make changes on the associated objects or uses them to perform a specific task.

Leave a Reply