The Wayback Machine - https://web.archive.org/web/20241123071333/https://www.geeksforgeeks.org/difference-method-function-python/
Open In App

Difference between Method and Function in Python

Last Updated : 26 Feb, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.


Previous Article
Next Article

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
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
6 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
Difference Between "Fill" and "Expand" Options for Tkinter Pack Method
Efficient layout management is crucial in creating visually appealing and user-friendly graphical user interfaces (GUIs). In the world of Python GUI programming, Tkinter stands out as a popular choice due to its simplicity and versatility. One of the key techniques used in Tkinter for layout management is the Pack method. In this article, we'll del
4 min read
Python set operations (union, intersection, difference and symmetric difference)
This article demonstrates different operations on Python sets. Examples: Input : A = {0, 2, 4, 6, 8} B = {1, 2, 3, 4, 5} Output : Union : [0, 1, 2, 3, 4, 5, 6, 8] Intersection : [2, 4] Difference : [8, 0, 6] Symmetric difference : [0, 1, 3, 5, 6, 8] In Python, below quick operands can be used for different operations. | for union. & for interse
1 min read
Difference between == and is operator in Python
When comparing objects in Python, the identity operator is frequently used in contexts where the equality operator == should be. In reality, it is almost never a good idea to use it when comparing data. What is == Operator?To compare objects based on their values, Python's equality operators (==) are employed. It calls the left object's __eq__() cl
3 min read
Python | Difference between iterable and iterator
Iterable is an object, that one can iterate over. It generates an Iterator when passed to iter() method. An iterator is an object, which is used to iterate over an iterable object using the __next__() method. Iterators have the __next__() method, which returns the next item of the object. Note: Every iterator is also an iterable, but not every iter
3 min read
Python | Difference between Pandas.copy() and copying through variables
Pandas .copy() method is used to create a copy of a Pandas object. Variables are also used to generate copy of an object but variables are just pointer to an object and any change in new data will also change the previous data. The following examples will show the difference between copying through variables and Pandas.copy() method. Example #1: Co
2 min read
Difference Between tf.Session() And tf.InteractiveSession() Functions in Python Tensorflow
In this article, we are going to see the differences between tf.Session() and tf.InteractiveSession(). tf.Session() In TensorFlow, the computations are done using graphs. But when a graph is created, the values and computations are not defined. So a session is used to run the graph. The sessions place the graphs on targeted devices and execute them
3 min read
Difference between Python and C#
Python and C# are two different programming languages that are used for different purposes. Here are some key differences between Python and C#: Syntax: Python and C# have different syntax. Python has a simpler and more straightforward syntax, which makes it easier to read and write. On the other hand, C# has a more complex syntax, which requires m
4 min read
Difference between C and Python
Here are some of the differences between C and Python. CPythonAn Imperative programming model is basically followed by C.An object-oriented programming model is basically followed by Python.Variables are declared in C.Python has no declaration.C doesn’t have native OOP.Python has OOP which is a part of the language.Pointers are available in C langu
2 min read
Difference between Python and Lua Programming Language
Python Python is one of the most popular and powerful scripting languages that works nowadays. It is a high-level interpreted programming language. It is a very simple scripting language and very easy to learn as compared to other languages. Python programming language is best for both scripting applications and as standalone programs along with th
3 min read
Difference between continue and pass statements in Python
Using loops in Python automates and repeats the tasks in an efficient manner. But sometimes, there may arise a condition where you want to exit the loop completely, skip an iteration or ignore that condition. These can be done by loop control statements. Loop control statements change execution from its normal sequence. When execution leaves a scop
3 min read
Difference between input() and raw_input() functions in Python
Developers often have a need to interact with users, either to get data or to provide some sort of result. Most programs today use a dialog box as a way of asking the user to provide some type of input. While Python provides us with two inbuilt functions to read the input from the keyboard. input ( prompt )raw_input ( prompt )input() function Pytho
4 min read
Difference between dict.items() and dict.iteritems() in Python
dict.items() and dict.iteriteams() almost does the same thing, but there is a slight difference between them - dict.items(): returns a copy of the dictionary’s list in the form of (key, value) tuple pairs, which is a (Python v3.x) version, and exists in (Python v2.x) version.dict.iteritems(): returns an iterator of the dictionary’s list in the form
3 min read
Python: Difference between Lock and Rlock objects
A thread is an entity within a process that can be scheduled for execution. Also, it is the smallest unit of processing that can be performed in an OS (Operating System). In simple words, a thread is a sequence of such instructions within a program that can be executed independently of other codes. For simplicity, you can assume that a thread is si
5 min read
Python - Difference between json.dump() and json.dumps()
JSON is a lightweight data format for data interchange which can be easily read and written by humans, easily parsed and generated by machines. It is a complete language-independent text format. To work with JSON data, Python has a built-in package called json. Note: For more information, refer to Working With JSON Data in Python json.dumps() json.
2 min read
Difference Between Python and Bash
Python and Bash both are both automation engineers' favorite programming language. But sometimes it may become difficult to choose any one of them. So you might be looking for articles telling which language to choose. But the honest answer is it depends on the task, scope, complexity of the task. Let's have a look at both languages. Python Python
3 min read
Difference between Yield and Return in Python
Python Yield It is generally used to convert a regular Python function into a generator. A generator is a special function in Python that returns a generator object to the caller. Since it stores the local variable states, hence overhead of memory allocation is controlled. Example: # Python3 code to demonstrate yield keyword # Use of yield def prin
2 min read
Difference between Python and C++
Python and C++ both are the most popular and general-purpose programming languages. They both support Object-Oriented Programming (OPP) yet they are a lot different from one another. In this article, we will discuss how Python is different from C++. What is Python?Python is a high-level, interpreted programming language. It was invented back in 199
4 min read
Difference between Python and Java
Programming languages play a fundamental role in computer science and are considered essential for the development of various applications. The two most popular programming languages in recent years have been Python and Java. Both are popular languages with numerous libraries, making it difficult to choose one. Python is gaining popularity because
4 min read
Difference Between x = x + y and x += y in Python
We often use x += y instead of x = x + y. So, are they same or different? Let's Find it here. Example 1: x = [1, 2] another_x = x y = [3] x += y print(x) print(another_x) Output: [1, 2, 3] [1, 2, 3] Example 2: x = [1, 2] another_x = x y = [3] x = x + y print(x) print(another_x) Output: [1, 2, 3] [1, 2] So here we find that both codes are almost sim
2 min read
Difference between NumPy.dot() and '*' operation in Python
In Python if we have two numpy arrays which are often referred as a vector. The '*' operator and numpy.dot() work differently on them. It's important to know especially when you are dealing with data science or competitive programming problem. Working of '*' operator '*' operation caries out element-wise multiplication on array elements. The elemen
2 min read
Difference between dir() and vars() in Python
As Python stores their instance variables in the form of dictionary belonging to the respective object both dir() and vars() functions are used to list the attributes of an instance/object of the Python class. Though having a similar utility these functions have significant individual use cases.dir() Function: This function displays more attributes
2 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg