Difference Between Method and Function in Python

1. Objective

In our journey with Python, we’ve seen 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.

Difference Between method 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

2

3

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

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

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.

DataFlair稿源:DataFlair (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 综合编程 » Difference Between Method and Function in Python

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录