Functions and methods are both callable code blocks in Python, but they operate differently. Functions are standalone blocks of code, while methods are functions bound to objects.
Definition: Functions and Methods
A function is a standalone block of reusable code that performs a specific task. Functions are called directly without reference to an object:
any([False, False, True])
len("Hello")
sum([1, 2, 3])
A method is a function that belongs to an object. Methods are called using dot notation on that specific object:
my_list = [1, 2, 3]
my_list.pop()
my_string = "hello"
my_string.upper()
The dot (.
) is the indicator that we are accessing a method attached to an object.
Methods are defined within a class definition. When we create an object (an instance) from that class, the object has access to all methods defined in its class. This principle applies to both built-in types and custom classes we write. For example, the list
type is defined as a class in Python's source code, and methods like .pop()
and .append()
are defined within that class. When we create a list object, we create an instance of the list
class and access its methods. The same applies to strings, dictionaries, and all other built-in types.
Key Difference: Object Attachment
The primary distinction between functions and methods lies in their attachment to objects. Methods are defined within a class and operate on instances of that class. Functions exist independently in the namespace.
Methods Belong to Specific Data Types
Each data type in Python has methods designed specifically for that type:
String methods:
text = "hello"
text.upper() # Returns "HELLO"
text.lower() # Returns "hello"
text.replace("l", "x") # Returns "hexxo"
List methods:
my_list = [1, 2, 3]
my_list.append(4) # Adds 4 to the list
my_list.remove(2) # Removes 2 from the list
my_list.pop() # Removes and returns the last item
Dictionary methods:
my_dict = {"name": "Alice", "age": 30}
my_dict.keys() # Returns the keys
my_dict.values() # Returns the values
Functions Work Across Multiple Data Types
Functions are designed as general-purpose tools that operate on multiple types of data:
len([1, 2, 3]) # Works with lists
len("hello") # Works with strings
len((1, 2, 3)) # Works with tuples
sum([1, 2, 3]) # Works with lists
sum((1, 2, 3)) # Works with tuples
any([False, False, True]) # Works with lists
any((False, True)) # Works with tuples
Mutating and Non-Mutating Operations
A critical distinction exists between methods and functions that modify data and those that do not.
Mutating Methods
Some methods alter the object they are called upon. The original object is modified:
my_list = [3, 1, 2]
my_list.sort() # Modifies my_list
print(my_list) # Output: [1, 2, 3]
my_list = [1, 2, 3]
my_list.pop() # Removes the last item
print(my_list) # Output: [1, 2]
Non-Mutating Methods and Functions
Other methods and most functions return a value or create a new object without modifying the original:
my_list = [1, 2, 3]
result = my_list.count(2) # Does not modify my_list
print(my_list) # Output: [1, 2, 3]
my_list = [3, 1, 2]
sorted_list = sorted(my_list) # Returns a new list
print(my_list) # Output: [3, 1, 2] - unchanged
print(sorted_list) # Output: [1, 2, 3] - new list
length = len([1, 2, 3]) # Returns a value
Key Takeaways
- Functions are standalone code blocks that can be called directly without reference to an object
- Methods are functions bound to specific objects and accessed using dot notation
- Functions typically work across multiple data types, while methods are specific to their data type
- Mutating operations modify the original object, while non-mutating operations return a new value or object without changing the original
- The dot (
.
) notation indicates that we are calling a method on an object - When choosing between a method and a function, consider whether the operation should modify the original data
Top comments (0)