in Technology by
What are Python namespaces? Why are they used?

1 Answer

0 votes
by

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.

Related questions

0 votes
    What is the order of namespaces in which Python looks for an identifier? a) Python first searches the built-in ... , then the local namespace and finally the built-in namespace...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    What are Context Variables and why they are used in Talend?...
asked Mar 18, 2021 in Technology by JackTerrance
0 votes
    What are negative indexes and why are they used?...
asked Dec 7, 2020 in Technology by JackTerrance
0 votes
    I have an XML file that starts like this: I'll have to open a lot of these files. Each of these ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 23, 2022 in Education by JackTerrance
0 votes
    So I have logical entities (person, country, etc.), GUI elements / controls, data and navigation ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 21, 2022 in Education by JackTerrance
0 votes
    So I have logical entities (person, country, etc.), GUI elements / controls, data and navigation ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 21, 2022 in Education by JackTerrance
0 votes
    So I have logical entities (person, country, etc.), GUI elements / controls, data and navigation ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 20, 2022 in Education by JackTerrance
0 votes
    So I have logical entities (person, country, etc.), GUI elements / controls, data and navigation ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 20, 2022 in Education by JackTerrance
0 votes
    What is lambda in Python? Why is it used?...
asked Dec 6, 2020 in Technology by JackTerrance
0 votes
    I am using exit(), quit(), os._exit(), sys.exit() to stop script execution, and I am confused which ... use to stop script execution? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me why Python is used in Data Science? Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    What is meant by thermonuclear reactions and why are they so called ? Why are these reactions not useful for peaceful purposes ? Select the correct answer from above options...
asked Jan 2, 2022 in Education by JackTerrance
0 votes
    When candidates have only the condition of age, why should they give other information to Election Commission? Please answer the above question....
asked Aug 14, 2022 in Education by JackTerrance
0 votes
    I'm trying to figure out Python lambdas. Is lambda one of those "interesting" language items that in ... maintenance coder nightmare. Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    Why Python have a string 'contains' substring method?...
asked Jan 9, 2021 in Technology by JackTerrance
...