locals and globals in function
Hi I'm wondering, I've seen this question in challenges, which went something like this: def bar(): x = 0 def foo(): print('x' in locals(), 'x' in globals()) foo() bar() The answer was clear to me.. Well it was until I started to play with this code: def bar2(): x = 0 def foo2(): print(x in locals(), x in globals()) foo2() bar2() gave the same answers and it was still clear, but than this happend: def bar3(): x = 0 def foo3(): print('x' in locals(), 'x' in globals(), x in locals(), x in globals()) foo3() bar3() why does the "'x' in locals()" return True? I thought, that 'x' is a string which quite obviously is not present in the foo() function in every option, and so is the x variable, which is only in the scope of bar function.. So every option should be False - what am I missing?