A namespace is a collection of currently defined symbolic names along with information to get the object that each name references. You can think of a namespace as a dictionary in which the keys are the object names and the values are the objects themselves. Each key-value pair maps a name to its corresponding object.
dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BaseExceptionGroup', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EncodingWarning', 'EnvironmentError', 'Exception', 'ExceptionGroup', 'False', 'FileExistsError', 'FileNotFoundError', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'ModuleNotFoundError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError', 'RecursionError', 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopAsyncIteration', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'WindowsError', 'ZeroDivisionError', 'build_class', 'debug', 'doc', 'import', 'loader', 'name', 'package', 'spec', 'abs', 'aiter', 'all', 'anext', 'any', 'ascii', 'bin', 'bool', 'breakpoint', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip']
Contains any names defined at the level of the main program.
The interpreter creates a new namespace whenever a function executes.
That namespace is local to the function and remains in existence until the function terminates.
Local, Enclosing, Global, Built-in.
The interpreter finds the value from the enclosing scope before looking in the global scope.
x = 'global'
def f():
x = 'enclosing'
def g():
print(x)
g()
f()
enclosing
Python resolves names using the LEGB (Local Enclosing, GLobal, Built-in) rule.
The LEGB rule dictates that the locally defined value of x
is searched first.
x = 'global'
def f():
x = 'enclosing'
def g():
x = 'local'
print(x)
g()
f()
local
Python really does implement these namespaces as dictionaries.
type(globals())
<class 'dict'>
Returns an actual reference (in x = globals()
, x
stays up to date).
my_name = 'j5py'
globals()["new_note"] = "Learn Python"
print(globals())
{'name': 'main', 'doc': None, 'package': None, ... , 'my_name': 'j5py', 'new_note': 'Learn Python'}
print(globals()["my_name"])
j5py
my_name is globals()["my_name"]
True
Returns a current copy (in x = locals()
, x
remains unchanged).
def f(x, y):
w = 'Hey'
print(locals())
f(10, 0.5)
{'x': 10, 'y': 0.5, 'w': 'Hey'}
x = 20
def f():
global x
x = 40
print(x)
f()
print(x)
40
40
def f():
x = 20
def g():
nonlocal x
x = 40
g()
print(x)
f()
40