Python local and global namespace usage instance, python global namespace
This example describes the usage of local and global namespaces in Python. Share it with you for your reference. The details are as follows:
X = 1def fun (a): B = 3 x = 4 def sub (c ): d = B global x = 7 print ("Nested Function \ n ========================") print locals () sub (5) print ("\ nFunction \ n ====================") print locals () ["x"] print globals () ["x"] print ("\ nGlobals \ n =============== ") print globals () fun (2) // scope. pyGlobals ======================={ 'X': 1, '_ file _': 'C: \ books \ python \ Users \ code \ scope. py', 'fun ': <function fun at 0x008D7570>, 't': <class' _ main __. t'>, 'time': <module 'time' (built-in)> ,...} nested Function ======================={ 'C': 5, 'B': 3, 'D ': 3} Function ========================={ 'A': 2, 'x': 4, 'B': 3, 'sub': <function sub at 0x008D75F0>} 47
I hope this article will help you with Python programming.