call by reference and call by value in python code example
Example: Is Python call by reference or call by value
If the object is mutable, the object will change.
eg:
def my_func(my_list):
my_list.append(5)
print("List printed inside the function: ",my_list)
numberlist = [1,2,3]
my_func(numberlist)
# numberlist here changed since list is mutable in python
If the object is immutable (like a string!),
then a new object is formed, only to live within the function scope.
eg:
def change_fruits(fruit):
fruit='apple'
myfruit = 'banana'
change_fruits(myfruit)
# myfruit does not change since string is immutable in python