Typefully

Namespaces in Python

Avatar

Share

 • 

4 years ago

 • 

View on X

⠿ Namespaces in Python ⠿ • What are Namespaces? • Why are they needed? • How does python implement them? ↓
• Namespace is a system that maintains the integrity of the variable names in a programming language. • It is a collection of all the names and the details of the objects they are associated with. • Namespaces are a way to implement scoping and variable scoping.
• The several properties like having unique variable names, multiple names pointing to the same objects are maintained using namespaces. • Namespaces help us in uniquely identifying all the names in a program.
• In Python namespaces are implemented using dictionaries. • The keys are the names while the values are the objects associated with those names. • They are used to map the names of their objects.
• There are four types of namespaces in Python 1. Built-In 2. Global 3. Local 4. Enclosing
• The Built-In has all the pre-defined python objects • The Global namespace contains all the names that are defined at the global level
• The local namespace is the namespaces within a block of code, like a function. • The enclosing namespace is the outer namespace available to a function that is defined inside another function.
Here's a short demonstration of the different namespaces:
This is how the namespaces work. Any comments, suggestions, or corrections are welcome. Thanks for reading!
Avatar

Prashant

@capeandcode

ml engineer @nagarro | python and ai