Python Namespace | Types of Python Namespace

Here, we have discussed about name and namespace in python programming language. A namespace is simple defined as a list or collection of unique...

Welcome guys! In this article we will going to discuss about name and namespace in Python programming language.

Python Namespace | Types of Python Namespace

What is Name in Python?

When we create any object in Python programming language we give name to that object. In python, we simply use variable name as name in our python programs. And as we earlier discussed in about python variable, we know that we can declare variable names and can assign them to various objects.

What is Python Namespace?

A namespace is simple defined as a list or collection of unique names and the details of each and every object referenced by those names in python. In simple terms, Namespace is a method to make sure that every name in the program are unique and can be used without causing any confusion or problem. In python, namespace are considered as dictionary.

Types of Python Namespace

There are four types of Python Namespace which are explained as follows;

Python Namespace | Types of Python Namespace

Global Namespace

In Global Namespace all names are defined at program, module or main level. In python, global namespace is created with the starting of python program and will destroy by the termination of python interpreter.

Example:

xvar1 = 5

xvar2 = 5


def add(var1, var2):

    temp = var1 + var2

    return temp

In the above example we have used and from global namespace.

Local Namespace

In Local Namespace, all those names are included which are defined for a function, class or block of code and are local to it. In python, local namespace is created with the execution of function and destroys with the termination of function.

Example:

xvar1 = 5

xvar2 = 5


def add(var1, var2):

    temp = var1 + var2

    return temp

In the above example, we have used the variable name , and which are defined in the local name space.

Enclosing Namespace

As we all are aware of the fact that we can define one function within another function and the function which is defined inside the other function can access namespace from the function which is outside. So, the namespace of the outer function is defined as the enclosing namespace.

Example:

xvar1 = 5

xvar2 = 5


def add(var1, var2):

    temp = var1 + var2


    def print_sum():

        print(temp)


    return temp

Here, function is defined inside the function so, the function is the enclosing namespace.

Built-in Namespace

In Built-in Namespace, all names of the built-in function and objects are included. These names are created when we start python interpreter and accessible through the program and destroyed when the the interpreter is closed. With the help of following command, we can access the list of all names in built-in namespace.

builtin_types = dir(__builtins__)

for types in builtin_types:

    print(types)
Output
ArithmeticError
AssertionError
AttributeError
BaseException
BlockingIOError
BrokenPipeError
BufferError
BytesWarning
ChildProcessError
ConnectionAbortedError
ConnectionError
ConnectionRefusedError
ConnectionResetError
DeprecationWarning
EOFError
Ellipsis
EnvironmentError
Exception
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
ZeroDivisionError
__build_class__
__debug__
__doc__
__import__
__loader__
__name__
__package__
__spec__
abs
all
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
licenselist
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
Here, it is visible that there are total 152 names defined in python built-in namespace.

Conclusion

Above we have discussed about name and namespace in python programming language. A namespace is simple defined as a list or collection of unique names and the details of each and every object referenced by those names in python. And there are four types of namespaces in python i.e., Global, Local, Enclosing and Built-in Namespace.