+ 1

Python, globals and locals

Why first output is False, and second True? def foo(): print(globals()==locals()) foo() print(globals()==locals())

22nd Jun 2020, 6:40 PM
Paolo De Nictolis
Paolo De Nictolis - avatar
3 Answers
+ 2
locals() returns you a dictionary of variables declared in the local scope while globals() returns you a dictionary of variables declared in the global scope. At global scope, both locals() and globals() return the same dictionary to global namespace. Try by printing locals() and globals() inside and outside the function.
22nd Jun 2020, 6:51 PM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar