I'm probably missing something really obvious, but how do I create a local copy of a global vaiable with the same name? I have something like:
d = {"one": 1, "two": 2, "three": 3}
def foo():
d = d.copy()
d["four"] = 4
I want to create a copy of d in the inner scope that I can modify as I like without affecting the global version. This will complain that d is reference before assignment, which makes perfect sense because it is being interpreted as a local variable. If I declare it global though, this will modify it.
This would be trivial if I didn't mind if it had a different name eg: d_local = d.copy()
, but that would be a big hassle in this situation and not just a find and replace one either.
If the only solution is to use a new name and deal with the consequences let me know.