0

Let's say I have a list L1.

I then define a function that passes L1 as an argument. This function does something like sort or append it, and returns the modified list. So I call the function with L1 and assign the result to variable a. Will L1 also be affected by this? I know that two variables can point to the same object and therefore affecting the object through one variable will affect the other variable as well, but I did not expected it happen even if the scope is different. Can someone explain?

  • 2
    Have you tried it out? :-) – erip Mar 20 '22 at 22:39
  • Why do you think the scope is relevant at all? **objects don't have a scope**, variables do. And variables are just names that refer to objects in source code. If you mutate an object using a variable in some scope, e.g. the loval scope of a function, why **wouldn't** it affect that object everywhere? – juanpa.arrivillaga Mar 20 '22 at 22:44

0 Answers0