The examples in this article describe Python local and global namespace usage. Share to everyone for your reference. as follows:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
x = 1 def fun (a): B=3 x=4 def Sub (c): d=b Global x = 7 print ("Nested functionn=================") print locals () sub (5) Print ("nfunctionn=================") print locals () print locals () ["X"] print globals () ["X"] Print ("nglobalsn======== ========= ") print globals () Fun (2)///scope.py globals ================= {' x ': 1, ' __file__ ': ' C:bookspythonch1codescope . 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>} 4 7 |
I hope this article will help you with your Python programming.