a = 1 # Uses global because there is no local 'a' def f(): print('Inside f() : ', a) # Variable 'a' is redefined as a local def g(): a = 2 print('Inside g() : ', a) # Uses global keyword to modify global 'a' def h(): global a a = 3 print('Inside h() : ', a) # Global scope print('global : ', a) f() print('global : ', a) g() print('global : ', a) h() print('global : ', a)
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter