CPython has two opcodes used for global variable lookups, LOAD_GLOBAL
and LOAD_NAME
. LOAD_NAME
looks for a local variable before a global variable, while LOAD_GLOBAL
goes straight to globals. LOAD_NAME
is primarily useful for class statements, but in the absence of a global
declaration, the compiler also happens to emit LOAD_NAME
for global variable lookups at module level.
Back before Python 3.4, LOAD_GLOBAL
used to say global name 'whatever' is not defined
when the lookup fails, and LOAD_NAME
used to say name 'whatever' is not defined
. This got changed when someone argued that "global" was confusing for cases where someone mistyped a local variable name.
You're on Python 2.7. When you run a variable lookup for a nonexistent name at top level, you get the LOAD_NAME
error message, but inside a function, you get the LOAD_GLOBAL
error message, which still says "global" on Python 2.