Python Tutorial

Python Variable

Python Operators

Python Sequence

Python String

Python Flow Control

Python Functions

Python Class and Object

Python Class Members (properties and methods)

Python Exception Handling

Python Modules

Python File Operations (I/O)

Python pass-by-value and pass-by-reference

In Python, the terms "pass-by-value" and "pass-by-reference" can be confusing, as Python's parameter passing model is different from languages like C++ or Java. Python uses a model called "pass-by-object-reference" (sometimes also called "pass-by-assignment"), which is a mix of both pass-by-value and pass-by-reference. Here's a tutorial on Python's parameter passing model:

  • Pass-by-object-reference

In Python, variables are references to objects, and when you pass a variable to a function, you are actually passing a reference to the object. This means that if the object is mutable (e.g., lists, dictionaries, sets), you can modify it within the function, and the changes will be reflected outside the function. However, if the object is immutable (e.g., numbers, strings, tuples), you cannot modify it within the function, and any changes you make will create a new object.

  • Immutable objects

Immutable objects include numbers, strings, and tuples. When you pass an immutable object to a function, you cannot modify the original object:

def modify_string(s):
    s += " world"

my_string = "hello"
modify_string(my_string)
print(my_string)  # Output: "hello"

In this example, my_string is not modified by the modify_string function because strings are immutable. When the function attempts to modify s, it creates a new string object instead.

  • Mutable objects

Mutable objects include lists, dictionaries, and sets. When you pass a mutable object to a function, you can modify the original object:

def modify_list(lst):
    lst.append(4)

my_list = [1, 2, 3]
modify_list(my_list)
print(my_list)  # Output: [1, 2, 3, 4]

In this example, my_list is modified by the modify_list function because lists are mutable. The function modifies the original list object by appending the number 4.

  • Reassigning references within a function

When you reassign a variable within a function, it does not affect the original object outside the function:

def reassign_list(lst):
    lst = [4, 5, 6]

my_list = [1, 2, 3]
reassign_list(my_list)
print(my_list)  # Output: [1, 2, 3]

In this example, the reassign_list function creates a new list object and assigns it to the lst variable within the function's scope. The original my_list remains unchanged.

In summary, Python uses a pass-by-object-reference model for parameter passing. When you pass a variable to a function, you are passing a reference to the object. If the object is mutable, you can modify it within the function, and the changes will be reflected outside the function. If the object is immutable, you cannot modify the original object, and any changes you make within the function will create a new object. When you reassign a variable within a function, it does not affect the original object outside the function.