Difference between Method and Function in Python

PythonServer Side ProgrammingProgramming

Function

A function is a block of code to carry out a specific task, will contain its own scope and is called by name. All functions may contain zero(no) arguments or more than one arguments. On exit, a function can or can not return one or more values.

Basic function syntax

def functionName( arg1, arg2,….):
   …….
   # Function_body
   ……..

Let’s create our own (user), a very simple function called sum(user can give any name he wants)”. Function “sum” is having two arguments called num1 and num2 and will return the sum of the arguments passed to the function(sum). When we call the function (sum) with values(arguments) 5 and 6, it returns 11.

def sum(num1, num2):
   return (num1 + num2)

Output

>>> sum(5,6)
11

So from above, we see the ‘return’ statement returns a value from python function.

The function allows us to implement code reusability. There are three kinds of functions −

  • Built-in functions ( As the name suggests, these functions come with the Python language, for example, help() to ask for any help, max()- to get maximum value, type()- to return the type of an object and many more.)

  • User-defined functions ( These are the functions that users create to help them, like the “sum” function we have created above).

  • Anonymous Functions (also called lambda functions and unlike normal function which is defined using def keyword are defined using lambda keyword).

Method

A method in python is somewhat similar to a function, except it is associated with object/classes. Methods in python are very similar to functions except for two major differences.

  • The method is implicitly used for an object for which it is called.

  • The method is accessible to data that is contained within the class.

General Method Syntax

class ClassName:
   def method_name():
      …………..
      # Method_body
      ………………

Let’s understand the method through one simple code −

 Live Demo

class Pet(object):
   def my_method(self):
      print("I am a Cat")
cat = Pet()
cat.my_method()

Output

I am a Cat

In the above code, we first defined class “Pet”. Then we created the object “cat” from this blueprint. Next, we call our custom method called my_method with the object(.i.e. cat).

Key differences between method and function in python

As we get the basic understanding of the function and method both, let's highlight the key differences between them −

  • Unlike a function, methods are called on an object. Like in our example above we call our method .i.e. “my_method” on the object “cat” whereas the function “sum” is called without any object. Also, because the method is called on an object, it can access that data within it.

  • Unlike method which can alter the object’s state, python function doesn’t do this and normally operates on it.

In Short, a method is a function which belongs to an object.

raja
Published on 19-Feb-2019 16:35:26
Advertisements