0% found this document useful (0 votes)
33 views

What Are Python Namespaces? Why Are They Used?: Lifecycle of A Namespace

Python namespaces are dictionaries that map names to objects, ensuring unique names and preventing conflicts. They allow the same name to reference separate objects. Namespaces include the local namespace of a function, the global namespace of imported modules, and the built-in namespace of core Python functions and exceptions. Namespaces are tied to the lifecycle of the objects they reference and end when the object's scope ends, so inner namespaces cannot be accessed from outer ones.

Uploaded by

ameetamarwadi
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

What Are Python Namespaces? Why Are They Used?: Lifecycle of A Namespace

Python namespaces are dictionaries that map names to objects, ensuring unique names and preventing conflicts. They allow the same name to reference separate objects. Namespaces include the local namespace of a function, the global namespace of imported modules, and the built-in namespace of core Python functions and exceptions. Namespaces are tied to the lifecycle of the objects they reference and end when the object's scope ends, so inner namespaces cannot be accessed from outer ones.

Uploaded by

ameetamarwadi
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 1

What are Python namespaces? Why are they used?

A namespace in Python ensures that object names in a program are unique and can be used
without any conflict. Python implements these namespaces as dictionaries with 'name as
key' mapped to a corresponding 'object as value'. This allows for multiple namespaces to use
the same name and map it to a separate object. A few examples of namespaces are as
follows:
• Local Namespace includes local names inside a function. the namespace is temporarily
created for a function call and gets cleared when the function returns.
• Global Namespace includes names from various imported packages/ modules that is being
used in the current project. This namespace is created when the package is imported in the
script and lasts until the execution of the script.
• Built-in Namespace includes built-in functions of core Python and built-in names for various
types of exceptions.
Lifecycle of a namespace  depends upon the scope of objects they are mapped to. If the scope of an object ends, the lifecycle of that namespace comes to an end. Hence, it isn't possible to access inner

namespace objects from an outer namespace.

You might also like