Understand the Key Difference Between Function and Method in Python!
Updated on Oct 09, 2025 | 4 min read | 8.38K+ views
Share:
For working professionals
For fresh graduates
More
Updated on Oct 09, 2025 | 4 min read | 8.38K+ views
Share:
Table of Contents
Did you know? Python 3.14 will introduce PEP 649 for deferred annotation evaluation, UUID support for versions 6, 7, and 8, plus improved error messages. With major updates like PEP 741 for a new C API and the removal of PGP signatures (PEP 761), Python is about to get even better! |
Functions and methods are fundamental concepts in Python programming. A function is a standalone block of code designed to perform a specific task, while a method is a function that is associated with an object or class. Understanding the difference between function and method in Python is essential for writing clean and efficient code.
In this guide, you'll read more about defining functions and methods, the Difference Between Function and Method in Python. We'll also cover practical use cases, and best practices for choosing between functions and methods in your Python programs.
Let's get straight to the point. The best way to understand the difference between function and method in Python is to see them compared side-by-side. While both are reusable blocks of code, their relationship with objects and how they are called are fundamentally different.
This table provides a comprehensive overview of their core distinctions.
Basis for Comparison | Function | Method |
Core Definition | A standalone block of code that performs a specific task. | A function that is defined inside a class and is bound to an object. |
Association | It is independent and does not belong to any class or object. | It is always associated with an object (an instance of a class). |
How It's Called (Invocation) | Called directly by its name: function_name(arg1, arg2) | Called on an object using dot notation: object.method_name(arg1, arg2) |
The self Parameter | Does not have a self parameter. It only knows about the data you pass to it. | Its first parameter is always a reference to the instance itself, conventionally named self. |
Data Access | Can only operate on the data passed to it as arguments or on global variables. | Can access and modify the object's internal data (its state or attributes) via self. |
State | It is stateless. It does not "remember" anything between calls. | It can modify the state of the object it belongs to, and that change persists. |
Analogy | A standalone kitchen tool, like a whisk. You can use it on any bowl of ingredients. | A button on an appliance, like the "start" button on a microwave. It only works on that specific microwave. |
Code Example | def add(a, b): return a + b | class Calculator: def add(self, a, b): ... |
This table captures the essential difference between method and function in python. Now, let's take a closer look at each of them to understand the concepts more deeply.
With 2025 bringing sweeping changes from automation, AI, and data science, Python stays at the forefront. Enhance your skills through these top courses and be ready for exciting career opportunities!
A function is the most basic unit of reusable code in Python. It's a self-contained block that you can define once and run many times. The key takeaway is that functions are independent. They don't need the context of a class or an object to operate.
A function is defined using the def keyword. Its structure includes:
Let's look at a simple, standalone function that greets a user.
Python
# Defining a standalone function
def greet_user(name):
"""This function takes a name and prints a greeting."""
message = f"Hello, {name}! Welcome."
print(message)
# Calling the function directly by its name
greet_user("Alex")
greet_user("Brenda")
Output:
Hello, Alex! Welcome.
Hello, Brenda! Welcome.
As you can see, greet_user exists on its own. We call it directly and give it the data it needs to work ("Alex"). It doesn't know about any objects or classes; it just performs its task and finishes. This independence is the hallmark of a function.
Also Read: How to Call a Function in Python?
Software Development Courses to upskill
Explore Software Development Courses for Career Progression
A method shares the same DNA as a function—it's a block of code defined with def—but it lives inside a class. This placement fundamentally changes its behavior. A method is intrinsically linked to an object (an instance of the class) and is designed to work with that object's data.
The most distinct feature of a method is its first parameter, self. When you call a method on an object, like my_object.do_something(), Python automatically passes my_object as the first argument to the method. The self parameter is there to receive this object. This gives the method the ability to read and change the object's attributes.
Let's create a BankAccount class. The actions you can perform—depositing, withdrawing—are perfect candidates for methods because they need to modify the account's balance.
Python
# Defining a class
class BankAccount:
def __init__(self, owner, balance=0):
self.owner = owner
self.balance = balance
# A method to deposit money
def deposit(self, amount):
self.balance += amount
print(f"Added {amount}. New balance is {self.balance}.")
# A method to withdraw money
def withdraw(self, amount):
if amount > self.balance:
print("Insufficient funds!")
else:
self.balance -= amount
print(f"Withdrew {amount}. New balance is {self.balance}.")
# Create an instance (object) of the class
my_account = BankAccount("John Doe", 1000)
# Call methods ON the object
my_account.deposit(500)
my_account.withdraw(200)
Output:
Added 500. New balance is 1500.
Withdrew 200. New balance is 1300.
Here, deposit and withdraw are methods. They use self to access and modify self.balance, the internal data of the my_account object. This ability to manage an object's state is the primary purpose of a method and a key difference between method and function in python.
Also Read: Understanding List Methods in Python with Examples
The distinction between functions and methods becomes even clearer when you look at Python's built-in tools.
Python provides many functions that are globally available. They are classic examples of standalone, independent functions.
Python
my_items = [10, 20, 30]
my_message = "Python"
# Calling built-in functions
print(f"The number of items is: {len(my_items)}")
print(f"The sum of items is: {sum(my_items)}")
print(f"The length of the message is: {len(my_message)}")
Notice that we pass the object (e.g., my_items) into the function.
Common data types like strings and lists are objects, and they come with a rich set of methods you can use.
Python
my_items = [4, 2, 8]
my_message = " hello world "
# Calling methods on a list object
my_items.append(1)
my_items.sort()
print(f"Modified list: {my_items}")
# Calling methods on a string object
cleaned_message = my_message.strip()
uppercase_message = cleaned_message.upper()
print(f"Modified message: {uppercase_message}")
Output:
Modified list: [1, 2, 4, 8]
Modified message: HELLO WORLD
Here, we call the method on the object (my_items.sort()). This is the most visible difference between function and method in Python in day-to-day coding.
Also Read: Module and Package in Python
While they are both callable blocks of code, the difference between function and method in Python is a cornerstone of object-oriented programming. The simplest way to remember it is: a method is a function that is bound to an object, while a function is independent.
This distinction appears in how they are called (dot notation for methods) and in the special self parameter that gives methods their power to manage an object's state. Understanding when to use a standalone function versus a class method is a sign of a maturing Python developer. Grasping this concept is a major milestone on your journey to writing clean, organized, and powerful object-oriented code.
Master in-demand Software Development skills like coding, system design, DevOps, and agile methodologies to excel in today’s competitive tech industry.
Stay informed with our widely-read Software Development articles, covering everything from coding techniques to the latest advancements in software engineering.
Subscribe to upGrad's Newsletter
Join thousands of learners who receive useful tips
No, by definition, a method is a function that is part of a class. A function defined outside of any class is simply called a function. This is the primary distinction between the two.
__init__ is a special method, often called a constructor. It's defined within a class and is automatically called when you create a new instance (object) of that class. It uses self to initialize the object's attributes.
No, you could technically name it anything you want (e.g., this, obj). However, self is a universally followed convention in the Python community. Using any other name is strongly discouraged as it makes the code confusing for other developers.
These are two special types of methods. A static method (decorated with @staticmethod) doesn't receive the instance (self) or the class as an implicit first argument. A class method (decorated with @classmethod) receives the class itself as the first argument, conventionally named cls.
Yes, this is possible and is known as a nested function or inner function. This is often used to create helper functions that are only relevant within the scope of the outer function or for creating closures.
You can call a method using the class name, but you must explicitly provide an instance as the first argument. For example, Car.display_info(my_car). This is what Python does behind the scenes, but it is not the standard or recommended way to call a method.
Methods that modify an object in-place (like list.sort() or list.reverse()) often return None by convention. This is to make it clear that the operation happened directly on the original object and did not create a new one. In contrast, the sorted() function returns a new, sorted list.
len() is a function that works on many different types of objects. The creator of Python, Guido van Rossum, decided that length is a general property of objects, so it should be a universal function rather than a method that every sequence class has to implement. This is a design choice in the language.
Yes, functions are "first-class citizens." This means you can treat them like any other object: you can assign them to variables, store them in data structures (like lists or dictionaries), pass them as arguments to other functions, and return them from other functions.
You use a method when the operation you are performing is conceptually tied to an object and needs to access or modify that object's internal state (its attributes). For example, a car.accelerate() method needs to modify the car's internal speed attribute.
Yes, functions can modify mutable data types (like lists or dictionaries) that are passed to them as arguments. However, they cannot modify immutable types like numbers, strings, or tuples passed to them (they can only return a new value).
When you access a method from an instance (e.g., my_car.display_info), you get a "bound" method object. This is an object that packages the function and the instance together. When you call it, the instance is automatically passed as the first argument.
No. Python has many built-in functions like print(), len(), and sum(). It also has many built-in types (like str, list, dict) that have their own built-in methods (.upper(), .append(), .keys()).
Yes. When you import a module, like the math module, you can call functions from it using dot notation, such as math.sqrt(). While this looks syntactically similar to a method call, math.sqrt() is still considered a function because math is a module, not an instance of a class.
No. The code for a method is stored only once in the class definition. Every object (instance) of that class simply holds a pointer back to the class's methods. The memory for each object is primarily used to store its unique attributes, not to duplicate the method code.
If you have a task that can be performed on some input data without needing to store any state, a function is a great choice. If you have a set of data (attributes) and behaviors (methods) that are logically grouped together, you should create a class.
A pure function is one that, for the same input, will always return the same output and has no side effects (like modifying a global variable or printing to the console). Python's math.sqrt() is a good example. Methods are often "impure" because their primary purpose is to change the object's state (a side effect).
A lambda is a small, anonymous function. It is defined without a name using the lambda keyword. Since it's not defined within a class, it is a type of function, not a method.
Yes. Since methods, once bound to an object, can be treated like any other object, you can store them in a list or other data structures. For example: actions = [my_car.start_engine, my_car.drive].
The concept is very similar. In strictly object-oriented languages like Java, nearly all "functions" are actually methods because they must be defined within a class. Python is more flexible, allowing you to mix procedural programming (with standalone functions) and object-oriented programming (with classes and methods).
900 articles published
Pavan Vadapalli is the Director of Engineering , bringing over 18 years of experience in software engineering, technology leadership, and startup innovation. Holding a B.Tech and an MBA from the India...
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
India’s #1 Tech University
Executive PG Certification in AI-Powered Full Stack Development
77%
seats filled
Top Resources