Python, globals and locals | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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
1 Answer
+ 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