Understanding how to "pass by reference" in Python involves recognizing that Python handles variables as references to objects, with these references passed by value. This concept can be a bit nuanced, so let's dive into examples that illustrate how this works with both mutable and immutable objects, and how we might simulate pass-by-reference behavior.
Example 1: Modifying a List Inside a Function
Lists are mutable objects, so changes to them inside a function are reflected outside of it.
def modify_list_contents(lst):
print('Before change:', lst)
lst.append('new item')
print('After change:', lst)
my_list = ['item1', 'item2']
print('Original list:', my_list)
modify_list_contents(my_list)
print('Modified list:', my_list)
Output:
Original list: ['item1', 'item2']
Before change: ['item1', 'item2']
After change: ['item1', 'item2', 'new item']
Modified list: ['item1', 'item2', 'new item']
Example 2: Attempting to Rebind a List Reference Inside a Function
Rebinding a list inside a function does not affect the original list outside.
def change_list_reference(lst):
print('Original inside function:', lst)
lst = ['new', 'list']
print('Rebound inside function:', lst)
my_list = ['item1', 'item2']
print('Before function call:', my_list)
change_list_reference(my_list)
print('After function call:', my_list)
Output:
Before function call: ['item1', 'item2']
Original inside function: ['item1', 'item2']
Rebound inside function: ['new', 'list']
After function call: ['item1', 'item2']
Example 3: Modifying a String Inside a Function
Strings are immutable, so we cannot change the original string in a function through assignment.
def modify_string(s):
print('Before change:', s)
s += ' and more text'
print('After change:', s)
my_string = 'Original text'
print('Before function call:', my_string)
modify_string(my_string)
print('After function call:', my_string)
Output:
Before function call: Original text
Before change: Original text
After change: Original text and more text
After function call: Original text
Example 4: Using a Wrapper to Simulate Pass by Reference for Immutable Object
We can use a list as a wrapper to simulate passing by reference.
def modify_using_wrapper(wrapper):
wrapper[0] = 'Modified content'
my_string = 'Original content'
wrapper = [my_string]
print('Before:', my_string)
modify_using_wrapper(wrapper)
print('After:', wrapper[0])
Output:
Before: Original content
After: Modified content
Example 5: Using a Class to Simulate Pass by Reference
Defining a simple class to wrap our variable allows us to modify its attributes, simulating pass-by-reference.
class Container:
def __init__(self, value):
self.value = value
def modify_object(obj):
obj.value = 'Modified content'
my_object = Container('Original content')
print('Before:', my_object.value)
modify_object(my_object)
print('After:', my_object.value)
Output:
Before: Original content
After: Modified content
These examples illustrate that while Python doesn't allow direct pass-by-reference like some other languages, you can manipulate mutable objects or simulate pass-by-reference behavior using wrappers or custom classes to achieve similar functionality.
Top comments (0)