cs107-lecture-examples

Example codes used during Harvard CS107 lectures
git clone https://git.0xfab.ch/cs107-lecture-examples.git
Log | Files | Refs | README | LICENSE

global_local.py (442B)


      1 #!/usr/bin/env python3
      2 def f(x):
      3     l0 = x  # l0 and x are local names
      4     l1 = g  # g is a global name
      5     print(f'id(x) = {id(x)}')
      6     print(f'id(g) = {id(g)}')
      7     print(f'Local vars in f(x):  {locals()}')
      8     print(f'Global vars in f(x): {globals()}')
      9 
     10 
     11 g = 42  # global name
     12 # local and global scopes are identical in `__main__`:
     13 # print(f'Local vars in `__main__`: {locals()}')
     14 # print(f'Global vars in `__main__`: {globals()}')
     15 f(g)