1

I have 2 functions. The one that's the second one being called, takes a local variable from the first function and modifies it. I would expect this to use copies and not modify the original local variable, but it seems to be modifying it.

Example:

def a():
    dictionary = {"world": "hello"} 
    b(dictionary)
    print(dictionary)
 
def b(dictionary):
    dictionary["world"] = "goodbye"

a() # prints {'world': 'goodbye'} while I expect {'world': 'hello'}

What am I missing, isn't b supposed to work on a copy?

bru53001
  • 121
  • 1
  • 3
  • 2
    No it doesn’t work on a copy unless you explicitly copy it. It’s not modifying the local *variable*, it’s modifying the *value* associated with that variable. – Mark Apr 11 '22 at 05:42
  • 2
    In Python, arguments are passed by *assignment*, not by value. See here https://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference – flakes Apr 11 '22 at 05:43

0 Answers0