In Python, all variables are passed by reference by default. This means that when you pass a variable to a function, you are actually passing a reference to the same object in memory, rather than a copy of the object.
Here’s an example that demonstrates this:
def my_function(my_list):
my_list.append(4)
print(my_list)
my_list = [1, 2, 3]
my_function(my_list)
print(my_list)
In this example, we define a function my_function
that takes a list as a parameter and appends the value 4
to the list. We call my_function
with the list [1, 2, 3]
, which is passed by reference to the function. After the function call, we print the modified list, which now contains the value 4
. We also print the original list, which has
- sunny asked 1 month ago
- You must login to post comments
Please login first to submit.