What I understand from reading the documentation is that Python has a separate namespace for functions, and if I want to use a global variable in that function, I need to us
This is explained well in the Python FAQ
What are the rules for local and global variables in Python?
In Python, variables that are only referenced inside a function are implicitly global. If a variable is assigned a value anywhere within the function’s body, it’s assumed to be a local unless explicitly declared as global.
Though a bit surprising at first, a moment’s consideration explains this. On one hand, requiring
global
for assigned variables provides a bar against unintended side-effects. On the other hand, ifglobal
was required for all global references, you’d be usingglobal
all the time. You’d have to declare asglobal
every reference to a built-in function or to a component of an imported module. This clutter would defeat the usefulness of theglobal
declaration for identifying side-effects.
https://docs.python.org/3/faq/programming.html#what-are-the-rules-for-local-and-global-variables-in-python