Home > Backend Development > Python Tutorial > How Does Python Handle Pass-by-Reference and Pass-by-Value?

How Does Python Handle Pass-by-Reference and Pass-by-Value?

Mary-Kate Olsen
Release: 2024-12-25 01:29:10
Original
362 people have browsed it

How Does Python Handle Pass-by-Reference and Pass-by-Value?

Pass-by-Reference in Python: A Comprehensive Explanation

In Python, passing arguments to functions is done by reference, meaning that the passed parameter is a reference to the actual object. However, it's important to understand the distinction between passing a reference by value versus actually passing by reference.

Passing a Reference by Value

In Python, parameters are passed by value, which means a copy of the object is assigned to the parameter within the function. This has two implications:

  1. Mutable objects: If the passed object is mutable (i.e., its contents can be changed), modifications made to the object within the function will be reflected in the original object outside the function. This is because both the function parameter and the original object reference the same underlying object in memory.
  2. Immutable objects: For immutable objects (i.e., whose contents cannot be changed), the original object cannot be modified from within the function. This is because the parameter in the function is a copy of the original object, and changes to the copy do not affect the original.

Simulating Pass-by-Reference in Python

Although true pass-by-reference is not directly supported in Python, there are several techniques to simulate it:

  1. Return the New Value: A function can return a new value that replaces the original object in the calling context. While not strictly pass-by-reference, this allows for effective modification of the original object.
  2. Use a Wrapper: A wrapper class or list can be used to hold the object and pass it by reference. Modifications made to the object within the function will affect the original object because the wrapper is passed by value, not the object itself.

Example:

The following code demonstrates pass-by-reference with a mutable (list) and an immutable (string):

# Mutable List
def modify_list(the_list):
    the_list.append('four')

outer_list = ['one', 'two', 'three']
print("Before: ", outer_list)
modify_list(outer_list)
print("After: ", outer_list)

# Immutable String
def modify_string(the_string):
    the_string = 'In a kingdom by the sea'

outer_string = 'It was many and many a year ago'
print("Before: ", outer_string)
modify_string(outer_string)
print("After: ", outer_string)
Copy after login

Output:

Before:  ['one', 'two', 'three']
After:  ['one', 'two', 'three', 'four']
Before:  It was many and many a year ago
After:  It was many and many a year ago
Copy after login

As seen in the output, the list is modified successfully (pass-by-reference), while the string remains unchanged (pass-by-value).

The above is the detailed content of How Does Python Handle Pass-by-Reference and Pass-by-Value?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template