#!/usr/bin/python3 # -*- coding: utf-8 -*- """ In a Python program, there are four types of namespaces: Built-In Global Enclosing (definition, not call) Local """ # Built-ins (some of these commands work differently, # run interactively versus in a debugger, # or other python interpreters like ipython. print(__builtins__) print(type(__builtins__)) print(dir(__builtins__)) # Works interactively: # __builtins__.print("hey") # in script form: __builtins__["print"]("hey") # Global x1 = "global" def f1() -> None: print(x1) def g1() -> None: print(x1) g1() f1() # Enclosing x2 = "global" def f2() -> None: print("print(x2) produces error if here, even though there is a global x2.") x2 = "enclosing" print(x2) def g2() -> None: print(x2) g2() f2() # Local x3 = "global" def f3() -> None: print("print(x3) produces error if here, even though there is a global x.") x3 = "enclosing" print(x3) def g3() -> None: print("print(x3) produces error if here, even though there is an enclosing x3.") print("print(x3) produces error if here, even though there is a global x3.") x3 = "local" print(x3) g3() f3()