"""
Pass arguments to a function that tries to change them.
"""
import sys
def f(i, s, li0, li1):
"Receive arguments and try to change their values."
print(f'f received {i}, "{s}", {li0}, {li1}.')
i = 20 #Put a new value into the variable i.
s = "goodbye" #Put a new value into the variable s.
li0 = [10, 20, 30] #Put a new value into the variable li0.
li1.append(30) #Append a new value to the list to which the variable li1 refers.
print(f'f changed them to {i}, "{s}", {li0}, {li1}.')
print()
myInt = 10
myString = "hello"
myList0 = [10, 20]
myList1 = [10, 20]
f(myInt, myString, myList0, myList1)
print("After returning from the function,")
print(f"myInt = {myInt}")
print(f'myString = "{myString}"')
print(f"myList0 = {myList0}")
print(f"myList1 = {myList1}")
sys.exit(0)
The following output shows that the function
f
was able to change the contents of the
list
to which the variable
myList1
refers.
This variable
li1
also refers to this
list.
f received 10, "hello", [10, 20], [10, 20]. f changed them to 20, "goodbye", [10, 20, 30], [10, 20, 30]. After returning from the function, myInt = 10 myString = "hello" myList0 = [10, 20] myList1 = [10, 20, 30]
set
to
f
and verify that
f
can change the contents of the
set,
just as
f
changed the contents
of the
list
to which the variable
myList1
refers.
In general,
f
can change the contents of any argument that is a
mutable
collection.
The mutable collections include
list,
set,
dictionary,
and others.