This article mainly introduces the usage of local and global namespaces in Python, and analyzes the usage skills of Python namespaces, for more information about how to use Python local and global namespaces, see the following example. 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 ':
,'T ':
, 'Time ':
,...} Nested Function ======================={ 'C': 5, 'B': 3, 'D ': 3} Function ========================={ 'A': 2, 'x': 4, 'B': 3, 'sub ':
} 47
I hope this article will help you with Python programming.