Python Namespace - Python Variable Scope With Examples

In this tutorial, we are going to learn about Python Namespace and variable scope. In our previous tutorial we learned about Python Custom Exception.

Python Namespace

Namespace is the naming system to avoid ambiguity and to make name uniques. If you’re familiar with other programming language (i.e C/C++/Java), you may know the namespace already.

However, Python’s namespace is implemented using Python Dictionary. That means, namespace is basically a key-value pair. For a given key, there will be a value. In the following sections, we will be discussing about names and their space.

Names and Their Space

If you dissect the word namespace, you will get two things. One is name and another is space. Basically, name is refer to the object name (also knows as identifier). That means, the object you declare is enlarge the namespace. And we have told earlier that the namespace in python is implemented using dictionary. So consider, there are two namespace, nameA and nameB. You can imagine them as

Here, you can see that, the names can be identical in both namespace but they are different as object. So, namespace allows us to use objects of same name but in different namespace. The following code will provide you the basic idea of namespace.

The produced output will be

So, you can see that two different object has same name but they are in different scope. Hence, their output differs.

Python Variable Scope

Actually, scope refers the coding region from where the objects of that scope can be accessed. That means, you cannot access the objects of a particular function from anywhere of your code. Like, the following code, you will see that you cannot access an object from outside of its scope.

So, you will see the output like below.

python-namespace-error

That means, you can call the function from the main scope but you can’t access the object of that function from the main scope. So, here comes the concept of scope. Basically, you can consider one indented block as its scope. This means, in the following code;

The output of the code will be;

Here, you can see that, the same indented blocks consist of the same namespace. So function_outer() function can be called from the main function’s scope while the function_inner() function can be called from the function_outer() function’s scope. You can also see that global variable can be accessed from the inner namespaces.

Lifetime of the Namespace

The lifetime of the namespace differs because they are created at different time. When the scope ends, the objects that are created in that scope usually deleted. That’s why, you cannot access objects offer inner namespace from the outer namespace because either they are not create yet or the namespace have already been deleted. But in the previous example, you see that you can access the objects from the outer namespace from the inner namespace.

So, that’s all about the Python Namespace. Hope, you understand well. If you have any query, please use the comment box.

References: Official Documentation

By admin

Leave a Reply