Why is it bad idea to modify locals in python?

What documentation says is that when you have a local x variable and do locals()['x'] = 42, then x may still point to the old object.

def foo():
    x = 0xABCD
    locals()['x'] = 42
    print(x)

foo()

Leave a Comment