The following code in Python 2.X prints "a : 2" as you'd expect:
def f():
#a = 1
exec "a = 2" in globals(), locals()
for k,v in locals().items(): print k,":",v
#a = 3
f()
But if you uncomment the "a = 1" then it prints "a : 1", as I didn't expect. Even weirder, if you uncomment the "a = 3" line then it doesn't print anything at all, which I definitely didn't expect (I had a baffling bug that I distilled down to that).
I think the answer is buried in the documentation on locals() and globals(), or maybe in other questions like this but I thought it was worth calling this manifestation out.
I'd love to learn what the Python interpreter is thinking here, as well as suggestions for workarounds.