Namespaces in Python is a system that will ensure the names (variables, functions etc) are unique and can be used without any conflict. Consider the below snippet
>>> my_var = 10
>>> type(my_var)
<class 'int'>
>>>
we get an instance of an integer class that has the value of 10 when this (my_var = 10
) statement runs. This instance is then labeled with the identifier my_var
. The relationship between an identifier and an object it identifies is implemented in python as namespaces and these namespaces are organised as dictionaries
for example here my_var = 10
can be imagined as a dictionary which looks something like {"name" : "object"} here object means value that is in the instance of Integer class. The terminology we use for namespaces is name to object mapping.
Consider the below snippet
>>> num_1 = 1
>>> num_2 = 2
>>> res = num_1 + num_2
>>> res
3
>>>
To keep track of the variable name, python will create a dictionary
with keys num_1, num_2, res and values(1,2,3) will be instance of the integer class.
if you are writing a Program that has only few lines of code then you can take care of the variable names to ensure that it is unique. But when working on large projects there might be files which has the exact same name that will result in name collisions and inconsistencies. Namespace helps to resolve this by referencing fulling qualified name in the code. Namespace is a bit lengthy topic. Hope you get some basic idea