Monday, January 11, 2021

Difference Between Function and Method in Python

If you have started learning Python after working in Java for many years you would have wondered over one thing; while in Java you always call an enclosed group of statements doing a specific task a method, in Python you would have noticed use of both functions and methods. If you think both terms can be used interchangeably then you must know there is a subtle difference between function and method in Python and that’s what is the topic of this post.

Function Vs Method in Python

While both function and methods in Python are written the same way as given below-

def function_name(param1, param2, ..) :
 """function doc string"""
 function suite

How these two differ is that a function in Python is a group of statements that can be written individually in a Python program. You can call that function using its name and passing arguments if any.

A function that is written with in a class and called using an object of the class or using class name is known as method. A method in Python is implicitly passed the object on which it is called using self.

You can understand it this way too, Python can be used to do both procedural as well as object oriented programming. When you are writing a procedural program then you can place common tasks in functions that are called using function name. When you are writing an object oriented program then you use methods to write the logic and call those methods using the objects of the class.

Example of function in Python

def sum(a, b):
  """This function adds the passed values"""
  sum = a + b
  return sum

# calling function
result = sum(5, 6)
print('Sum is', result)

Output

Sum is 11

Here we have a function called sum that takes two arguments and return the sum of those two arguments. You call the function using its name.

Example of method in Python

class Person:
  '''Class Person displaying person information'''
  #class variable
  person_total = 0
  def __init__(self, name, age):
    print('init called')
    self.name = name
    self.age = age
    Person.person_total +=1

  def display(self):
    print(self.name)
    print(self.age)

# printing doc string
print(Person.__doc__)
# creating class instances
person1 = Person('John', 40)
person1.display()
person2 = Person('Lila', 32)
person2.display()
print('Count- ', Person.person_total)

Output

Class Person displaying person information
init called
John
40
init called
Lila
32
Count- 2

That's all for this topic Difference Between Function and Method in Python. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Python Tutorial Page


Related Topics

  1. Passing Object of The Class as Parameter in Python
  2. Python Functions : Returning Multiple Values
  3. Constructor in Python - __init__() function
  4. pass Statement in Python
  5. Local, Nonlocal And Global Variables in Python

You may also like-

  1. Namespace And Variable Scope in Python
  2. Comparing Two Strings in Python
  3. Convert String to int in Python
  4. Python Program to Find Factorial of a Number
  5. Find Minimum and Maximum Value Nodes in Binary Search Tree - Java Program
  6. Invoke Method at Runtime Using Java Reflection API
  7. Difference Between Encapsulation And Abstraction in Java
  8. Spring Integration With Quartz Scheduler