Python - Namespace

python logo pixel art

Python namespace is a mapping of unique names to objects related to scope.


Namespace vs Scope


Types of Namespace

python namespace type
y = 10
def doSomething(x):
  z = 32
  print(locals()) # {'x': 10, 'z': 32}
  return x + y + z

doSomething(10)
print(globals()) # {'__name__': '__main__', ..., 'y': 10, 'doSomething': <function doSomething at 0x000>}

globals() - Get the global namespace dictionary.
locals() - Get the current scope namespace dictionary.