Open In App

Difference between Method and Function in Python

Last Updated : 26 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Here, key differences between Method and Function in Python are explained. Java is also an OOP language, but there is no concept of Function in it. But Python has both concept of Method and Function.

Python Method

  1. Method is called by its name, but it is associated to an object (dependent).
  2. A method definition always includes ‘self’ as its first parameter.
  3. A method is implicitly passed to the object on which it is invoked.
  4. It may or may not return any data.
  5. A method can operate on the data (instance variables) that is contained by the corresponding class

Basic Method Structure in Python : 

Python




# Basic Python method
class class_name
    def method_name () :
        ......
        # method body
        ......  


Python 3 User-Defined Method : 

Python3




# Python 3  User-Defined  Method
class ABC :
    def method_abc (self):
        print("I am in method_abc of ABC class. ")
 
class_ref = ABC() # object of ABC class
class_ref.method_abc()


Output:

 I am in method_abc of ABC class

Python 3 Inbuilt method : 

Python3




import math
 
ceil_val = math.ceil(15.25)
print( "Ceiling value of 15.25 is : ", ceil_val)


Output:

Ceiling value of 15.25 is :  16

Know more about Python ceil() and floor() method.

Functions

  1. Function is block of code that is also called by its name. (independent)
  2. The function can have different parameters or may not have any at all. If any data (parameters) are passed, they are passed explicitly.
  3. It may or may not return any data.
  4. Function does not deal with Class and its instance concept.

Basic Function Structure in Python : 

Python3




def function_name ( arg1, arg2, ...) :
    ......
    # function body
    ......  


Python 3 User-Defined Function : 

Python3




def Subtract (a, b):
    return (a-b)
 
print( Subtract(10, 12) ) # prints -2
 
print( Subtract(15, 6) ) # prints 9


Output:

-2
9

Python 3 Inbuilt Function : 

Python3




s = sum([5, 15, 2])
print( s ) # prints 22
 
mx = max(15, 6)
print( mx ) # prints 15


Output:

22
15

Know more about Python sum() function. Know more about Python min() or max() function.

Difference between method and function

  1. Simply, function and method both look similar as they perform in almost similar way, but the key difference is the concept of ‘Class and its Object‘.
  2. Functions can be called only by its name, as it is defined independently. But methods can’t be called by its name only, we need to invoke the class by a reference of that class in which it is defined, i.e. method is defined within a class and hence they are dependent on that class.


Similar Reads

Difference between Method Overloading and Method Overriding in Python
Method Overloading: Method Overloading is an example of Compile time polymorphism. In this, more than one method of the same class shares the same method name having different signatures. Method overloading is used to add more to the behavior of methods and there is no need of more than one class for method overloading.Note: Python does not support
3 min read
Difference between __sizeof__() and getsizeof() method - Python
Memory management is of utmost priority when we write large chunks of code. So in addition to good coding knowledge, it is important to be able to write programs, so as to handle memory efficiently. So let us look at the two ways of getting the size of a particular object in Python. These are getsizeof() method and __sizeof() method. The getsizeof(
2 min read
Python | Difference between two dates (in minutes) using datetime.timedelta() method
To find the difference between two dates in Python, one can use the timedelta class which is present in the datetime library. The timedelta class stores the difference between two DateTime objects. To find the difference between two dates in the form of minutes, the attribute seconds of timedelta object can be used which can be further divided by 6
2 min read
Difference between Function and Procedure
Pre-requisites: What is SQL? Structured Query Language is a computer language that we use to interact with a relational database.SQL is a tool for organizing, managing, and retrieving archived data from a computer database. In this article, we will see the difference between Function and Procedure. Function:The function is one of the fundamental th
3 min read
Difference between Normal def defined function and Lambda
In this article, we will discuss the difference between normal def defined function and lambda in Python. Def keyword​​​​​​​In python, def defined functions are commonly used because of their simplicity. The def defined functions do not return anything if not explicitly returned whereas the lambda function does return an object. The def functions m
2 min read
Difference between Generator and Normal Function
Normal functions in Python are used for traditional computation tasks, with execution proceeding from start to finish, typically returning a single result. On the other hand, generator functions employ the `yield` statement to produce values lazily, preserving their state across multiple calls. This allows generators to efficiently handle large dat
4 min read
Difference between modes a, a+, w, w+, and r+ in built-in open function?
Understanding the file modes in Python's open() function is essential for working with files effectively. Depending on your needs, you can choose between 'a', 'a+', 'w', 'w+', and 'r+' modes to read, write, or append data to files while handling files. In this article, we'll explore these modes and their use cases. Difference between modes a, a+, w
4 min read
Class Method vs Static Method vs Instance Method in Python
Three important types of methods in Python are class methods, static methods, and instance methods. Each serves a distinct purpose and contributes to the overall flexibility and functionality of object-oriented programming in Python. In this article, we will see the difference between class method, static method, and instance method with the help o
5 min read
Difference between 'and' and '&' in Python
and is a Logical AND that returns True if both the operands are true whereas '&' is a bitwise operator in Python that acts on bits and performs bit-by-bit operations. Note: When an integer value is 0, it is considered as False otherwise True when used logically. and in PythonThe 'and' keyword in Python is used in the logical operations. It is u
4 min read
Difference between reshape() and resize() method in Numpy
Both the numpy.reshape() and numpy.resize() methods are used to change the size of a NumPy array. The difference between them is that the reshape() does not changes the original array but only returns the changed array, whereas the resize() method returns nothing and directly changes the original array. Example 1: Using reshape() C/C++ Code # impor
1 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg