What is the difference between a method and a function in python?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

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

    class class_name

        def method_name [] :

            ......

            ......   

    Python 3 User-Defined Method : 

    Python3

    class ABC :

        def method_abc [self]:

            print["I am in method_abc of ABC class. "]

    class_ref = ABC[]

    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, ...] :

        ......

        ......   

    Python 3 User-Defined Function : 

    Python3

    def Subtract [a, b]:

        return [a-b]

    print[ Subtract[10, 12] ]

    print[ Subtract[15, 6] ]

    Output:

    -2
    9

    Python 3 Inbuilt Function : 

    Python3

    s = sum[[5, 15, 2]]

    print[ s ]

    mx = max[15, 6]

    print[ mx ]

    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.

    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.

    Updated on 30-Jul-2019 22:30:24

    • Related Questions & Answers
    • Difference between Method and Function in C#
    • Difference between Function and Procedure
    • What is the difference between a method and a function?
    • What is the difference between function overriding and method hiding in C#?
    • Difference between SCALAR and COLUMN function
    • Difference between == and .Equals method in c#
    • Difference between == and equals[] method in Java.
    • Difference between constructor and method in Java
    • Difference between Method Overriding and Method Hiding in C#
    • Difference Between Friend Function and Friend Class
    • Difference Between Virtual and Pure Virtual Function
    • Difference between Function and Predicate in Java 8
    • Difference Between Function Overloading and Overriding in C++
    • Difference between Python and PHP.
    • Difference between Python and Bash

    What is the difference between a method and a function?

    A function is a set of instructions or procedures to perform a specific task, and a method is a set of instructions that are associated with an object.

    Is it called method or function in Python?

    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.

    What are the three main differences between a method and a function?

    Method and a function are the same, with different terms. A method is a procedure or function in object-oriented programming. A function is a group of reusable code which can be called anywhere in your program. This eliminates the need for writing the same code again and again.

    Chủ Đề