Difference Between Method and Function in Python

Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python

In our journey with Python, we’ve seen Python methods and functions. But don’t they feel the same? Well then, time to make a comparison between Python functions and Methods with examples. But before that, let’s revise both function and method in Python.

So, let’s start with the difference between a method and a function in Python.

Python Functions – A Revision

A function in Python is a sequence of statements that execute in a certain order, given a name. They let us implement code reusability. A function is a stand-alone block of code you call by name, like len(data). It lives at the top level of a module and doesn’t belong to any specific object.

When we discussed Python Functions, we covered built-in and user-defined functions.

a. User-Defined Functions in Python

Like any other programming language, Python lets us define our own functions. Let’s take an example.

>>> def add(a,b):
          return a+b 
>>> add(3,-3)

0

We call this function ‘add’. As expected, it adds two values, which it takes as arguments. When we call it with values -3 and 3, it returns 0. And as you can see, the ‘return’ statement returns a value from a Python function.

These let us create a modular program, which in turn lets us make changes easily whenever we want.

To learn more about functions, docstrings, and scope and lifetime, read up on Python Functions.

b. Built-in Functions in Python

In our write-up on Python In-Built Functions, we discussed about 66 functions in brief. These are the functions that Python provides us with, and some of these we see and use commonly. Let’s take a look at a program that uses some of these.

>>> def demofunc(a,b):
	"""
//This function is to demonstrate a few built-in functions in Python
	"""
	print("Begin")
	print(max(a,b))
	print(abs(a),abs(b))
	print(float(a),b)
	print(callable(a))
	print(hash(a),hash(b))
	print(len('ab'))
	print(type(a))
	for i in range(2,4): print(i)
>>> demofunc(2,3)

Begin

3

2 3

2.0 3

False

2 3

2

<class ‘int’>

2

3

>>> c=lambda :print("Hi")
>>> type(c)

<class ‘function’>
Also see, ython Lambda Expressions.

Python Methods – A Revision

A method is a function tied to an object; you invoke it with dot syntax, such as data.append(5) on a list.

Because a method is linked to its object, Python silently passes that object as the first argument—commonly named self in class definitions. This bond lets the method read or modify the object’s own data, whereas a plain function can only access what you hand it directly.

Let’s take an example.

>>> class vehicle:
	def __init__(self,color):
		self.color=color
	def start(self):
		print("Starting engine")
	def showcolor(self):
		print(f"I am {self.color}")
>>> car=vehicle('black')
>>> car.start()

Starting engine

>>> car.showcolor()

I am black

First, we defined the class ‘vehicle’. Then, we created the object ‘car’ from this blueprint. Here, __init__() is a magic method that serves as a constructor for the class. Method start() and showcolor() let us do what we want to. Remember that Python methods must have a parameter ‘self’ to allow them to refer to the current object.

Comparison Between Method and Function in Python

Now that we’ve revised the Python method and function, we can compare them. Let’s list down the major differences.

1. A Python method is called on an object, unlike a function. In our example above, we call start() on the object ‘car’. Conversely, we call a Python function quite generically- we don’t call it on any object. Since we call a method on an object, it can access the data within it.

2. A method may alter an object’s state, but a Python function usually only operates on it, and then prints something or returns a value.

So, this was all about the difference between method and Function in Python. Hope you like our explanation

Conclusion

After this discussion, we conclude that there’s a thin line between methods and functions in Python, with examples. In simple terms , understanding this difference helps you write an organised code.

While learning Python, you will learn when to use a function or a method, which makes the program easier to read and maintain.
See also- Python Closure & Python Range Function
For reference

You give me 15 seconds I promise you best tutorials
Please share your happy experience on Google

courses

DataFlair Team

DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.

3 Responses

  1. Krystal says:

    I love this explanation!

    • DataFlair Team says:

      It’s a pleasure for us to learn that our efforts have been highly beneficial to you. If you’ve found the course enjoyable, we would kindly request you to rate our services on google.

Leave a Reply

Your email address will not be published. Required fields are marked *