Sunday, May 12, 2024

📑 Referring to the Same Object

📑 Task

1) Will the result of printing the values for the functions f1 and f2
be the same in all cases?
2) Print out the values of the function f2 at x = 1, 2, 3 so
that the result will be the same for all x

📑 Answer

1) When printed in a loop, the functions' values will be equal
For f1 the default list-argument changes
(it was set when the function was defined)
For f2 the same list-variable changes
(it was set as a global variable)
When printed by the generator, the functions' values become different
f1 continues to expand the default list-argument
f2 begins to expand the empty list-variable again
2)