Functions and methods have a lot in common, actually, a method is literally a function that is associated with an object. This may be a little confusing especially for beginners and that is why in this article we will seek clarity on the distinction between functions and methods.

function vs method

By definition, a function is a collection of statements that can get executed together to perform a certain action. A method, on the other hand,  is a special type of a  function that is associated with a specific object or class.

The name 'function' is usually used to refer to standalone blocks of code that perform a specific task.

ExampleEdit & Run

Example of a function

def add(a, b):
    print(f'{a} + {b} = {a + b}')

#call the function
add(10, 20)
Output:
10 + 20 = 30[Finished in 0.010647122981026769s]

The add() above is a standalone function, we would therefore use the name 'function' to refer to it rather than a method.Let us now consider an example with a method.

ExampleEdit & Run

Example of a method

class MyClass:
    def add(self, a, b):
        print(f'{a} + {b} = {a + b}')

# create a class object
obj = MyClass()

#call the method
obj.add(10, 20)
Output:
10 + 20 = 30[Finished in 0.011175824096426368s]

On the above example, we have a class named MyClass, we have defined a "method"  called add() inside the class. In order to use the method, we had to instantiate an object of the class i.e obj

Some things to note about functions:

  • A function does not need a class or an object.
  • Functions are independent entities in a program.
  • Functions do not require any mandatory arguments such as self. They take arguments based on the parameters specified in their declaration.
  • We can invoke a function by simply using its name.
  • Functions are not associated with any objects.

Some things to note about methods:

  • Methods are always bound to an object or a class.
  • Methods are dependent on the class they belong to.
  • To use a method we must pass through its class all the bound object.
  • A method may need a mandatory  first argument such as self or cls

As long as a function appears inside a class or we have to use another object  in order to access it, we call it a method. There are three types of methods i.e instance, class and static methods you can follow the link to see more details about each of them.

Builtin functions and methods

Python comes with a number of builtin functions and methods.

Some common builtin functions includes print(), len(), min(), max(), etc

ExampleEdit & Run
data = [5, 7, 1, 10]

#Using builtin functions
print(len(data))
print(max(data))
print(min(data))
Output:
4101[Finished in 0.01055221795104444s]

On the other hand, buitin methods are mostly associated with core data types such as lists, tuples , strings etc. Each data types have some methods associated with them that are relevant to them and can be used to manipulate its objects.

ExampleEdit & Run

some string methods

mystring = 'Pynerds'

#call string methods
print(mystring.upper()) #transform the string to uppercase
print(mystring.lower()) #transform the string to lowecase
print(mystring.swapcase()) #swap the cases of the string characters
Output:
PYNERDSpynerdspYNERDS[Finished in 0.010224402882158756s]
ExampleEdit & Run

some list methods

#Using list methods

mylist = [0, 2, 1, 4, 5, 3]

#sort the list
mylist.sort()
print(mylist)

#add an element
mylist.append(6)
print(mylist)

#remove the first element
mylist.pop(0)
print(mylist)
Output:
[0, 1, 2, 3, 4, 5][0, 1, 2, 3, 4, 5, 6][1, 2, 3, 4, 5, 6][Finished in 0.010168029926717281s]