-1

What do you understand by the term namespace in Python? What are python namespaces?

I tried understanding this topic...namespaces in python but was finally unable to understand it clearly..so want to learn much more about the topic from the python programming language...

A Python namespace 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 its respective 'object as value'.

this is what i tried to understand but not able to understand it...

2 Answers2

1

Here is an explanation for your question.

I would further suggest reading on Python Tutorial for namespace and module

It is better to first understand what is module then you could easily understand what is namespace.

And also for an extra information, namespace has 3 category as shown below: enter image description here

0

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

Aswin A
  • 74
  • 3