Difference Between Method and Function in Python | Python Method Vs Function

Free Python courses with 57 real-time projects - Learn Python

1. Python Method Vs Function – Objective

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

So, let’s start Difference Between Method and Function in Python.

Difference Between Method and Function in Python | Python Method Vs Function

Difference Between Method and Function in Python | Python Method Vs Function

Difference Between methods and function in python

2. Python Functions – A Revision

Python function is a sequence of statements that execute in a certain order, given a name. They let us implement code reusability. When we talked about Python Functions, we talked about built-in and user-defined functions.

a. User-Defined Functions

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 Python function.

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

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

b. Built-in Functions

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.

3. Python Methods – A Revision

Python method is like a function, except it is attached to an object. We call a method on an object, and it possibly makes changes to that object. A method, then, belongs to a class. 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 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 method must have a parameter ‘self’ to allow them to refer to the current object.

4. 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. Python method is called on an object, unlike a function. In our example above, we call start() on the object ‘car’. Conversely, we call 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 Python function usually only operates on it, and then prints something or returns a value.

So, this was all about Difference Between Method and Function in Python. Hope you like our explanation

5. Conclusion

After this discussion, we conclude that there’s a thin line between method and function in python with examples. The only major difference is that we call Python method on an object, but it’s not the same with functions. Also, methods may modify an object; Python functions don’t.
See also- Python Closure & Python Range Function
For reference

If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

follow dataflair on YouTube

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 *