Scope, local, global, builtin

Name resolution happens in three stages (LGB):

local Names that are defined inside a function have local scope. The function locals() returns a dictionary containing the local symbol table.

The function inspect.getmodule( object) (9.11) returns the objects module.

global Modules create global namespaces. The global statement which is executed inside a function makes module names accessible. The function globals() returns a dictionary containing the modules global symbol table.

builtin Pre-defined Python names are in the module __builtin__.

Here is an example for local and global name references:

#!/usr/bin/env python

x = 1
y = 2

def func():
    global x    # necessary becaus we want to change the global x
    x = 10      # changes the global x
    z = x + y   # uses the global y
    return z

print( func())
print( " x:", x)  # changed to 10