Questions

What are Python Namespaces why are they used?

Python Namespaces are collections of different objects that are associated with unique names whose lifespan depends on the scope of a variable. The scope is a region from where we can access a particular object. There are three levels of scopes: built-in (outermost), global, and local. We can only access an object if its scope allows us to do so. 

For example:

                    

# global namespace
var_a = 75
def A_func():
 
    # local namespace
    var_b = 38
    def B_inner_func():
 
        # nested local 
	# namespace
        var_c = 24

We cannot access the variable var_a if we refer to it in the nested local namespace (where var_c is present). If we try to assign a new value to var_a from here, a new variable gets created in the nested local namespace.

Related Questions

Related Topics

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.