A namespace refers to a naming system that is used to ensure that all object names in a Python program are unique, to avoid any conflicts. In Python, these namespaces are implemented as dictionaries with ‘name as key’ mapped to a corresponding ‘object as value.’ As a result, multiple namespaces can use the same name and map it to a different object.
Below are the three types of namespaces in Python:
- Local namespace – It includes local names inside a function. A local namespace is temporarily created for a function call and is cleared when the function returns.
- Global namespace – It consists of the names from various imported packages/ modules that are currently being used in a project. A global namespace is created when a package is imported in the script, and it lasts until the script is executed.
- Built-in namespace – It includes built-in functions of core Python and built-in names for the different types of exceptions.