0

This function receives a dict as a keyword argument. It then prints this dict, and updates it >after<.

When I run it the first time, without passing arguments, the dict is, obviously, empty. But when I run the same function again, also without passing arguments, the dict is not empty, and has the data that was stored in the first call.

Shouldn't these two objects be independent? If not, how can I make them such, without passing arguments and without copying the object inside the function?

(this is necessary for my usecase, since I want to share a memory recursively, but not between different starting points, and, unfortunately, the function call won't pass this argument).

Thanks

def a(b={}):
    print(b)
    b['c'] = 'd'

a() # {}
a() # {'c': 'd'}
richard
  • 22
  • 1
Enzo Dtz
  • 361
  • 1
  • 16
  • 2
    Try this: `def a(b=None): if b is None: b = {} print(b) b['c'] = 'd' ` – Byte Ninja Jul 23 '23 at 02:46
  • This is an extremely common question and the solution you're looking for does exist in the dupe: https://stackoverflow.com/a/11416002/476 – deceze Jul 23 '23 at 07:27

0 Answers0