2

I'm working on a script that requires manipulation of locals(), and I'm having trouble removing values from it. I've tried locals().pop(key) and del locals()[key], but neither works.

As an example:

def locals_test():
    a, b = 1, 2
    locals().pop('a')
    del locals()['b']
    return locals()

def dict_test():
    test = {'a':1, 'b':2}
    test.pop('a')
    del test['b']
    return test

print(locals_test()) # --> {'a':1, 'b':2}
print(dict_test())   # --> {}

I'm trying to replicate the behavior of dict_test() in locals_test(), but I've yet to find a solution. Does anyone know how to solve this?

martineau
  • 119,623
  • 25
  • 170
  • 301
aedificatori
  • 119
  • 1
  • 11
  • 3
    Changes to the dictionary returned by `locals` will *not affect the local namespace*. Did you read the documentation for `locals`? Why do you believe you even need to manipulate `locals`??? – juanpa.arrivillaga Jan 01 '19 at 23:03
  • 2
    `locals()` is a one-way street. You can't remove local variables by deleting from the `locals()` dictionary. – Martijn Pieters Jan 01 '19 at 23:09
  • I missed that part of the documentation, looks like. It didn't occur to me that the returned dictionary would be by reference as opposed to the actual objects. As for why I'm even doing this, I'm working on a Python interface written in Python, and I'm pulling locals between snippets of code. – aedificatori Jan 01 '19 at 23:13
  • 1
    "by reference as opposed to the actual objects" What? No, that isn't what's going on. `locals` simply *builds a dict on each invocation* from the local namespace. It returns that `dict`, but local namespaces aren't affected by modifying that dict. But that dict contains the actual objects being referenced in the local namespace. Anyway, that does seem like a good reason to use `locals`. – juanpa.arrivillaga Jan 01 '19 at 23:20
  • Ahh, sorry, I used the wrong words for that. Build by invocation makes a lot of sense, though, thank you for clarifying. I appreciate it. – aedificatori Jan 01 '19 at 23:28

1 Answers1

3

locals() returns a dictionary which is a copy of the local variables. Changing a copy won't remove the variable. The way that works:

def locals_test():
    a, b = 1, 2
    del a
    del b
    return locals()

General advice: playing with locals & globals like this is often the symptom of a XY problem. You'd be better off with a dictionary of values so you're not polluted by the rest of the local variables.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219