MOBI BOOT CAMP CORP. logoLearning Buddy
  • SIGN IN
  • Introduction
  • Namespace and Scope
  • The Class
  • Context Managers
  • Inheritance
  • Modules and Packages
  • Virtual Environment
  • Flask
  • Handling Forms with Flask-WTF
  • Jinja
  • Structuring a Flask App
  • Intro to Datastore
  • Intro to AppEngine
  • Flask on App Engine
  • Dash
  • Deploying a Dash App
  • MS Sql Server on Docker

Scope and Namespace

What is a Namespace?

A namespace is a system that provides a unique name for every object in Python. In simple terms a namespace is a dictionary in which the keys are the object names and the values are the objects themselves. The built-in functions that we have been using all along are added to the program's namespace and can be accessed directly by their names; e.g., print() etc. These built-in functions also belong to the builtin module.

Namespaces are created and destroyed at different moments when a program is executed and have different lifecycles:

Points to note

* The namespace containing the built-in names is created when the Python interpreter starts up and remains in memory until the program exits.
  • The global namespace for a module is created when the module is imported with or without a name and remains in memory until the program exits.
  • When a function is called, a local namespace is created for that function. This namespace exists only until the function returns or raises an unhandled exception.

Accessing namespaces with and without qualifiers

To understand namespaces better let us write a simple program as shown below

Save this program in mylib.py

a = 10

In the above program, 'a' is a variable in the Global Namespace

What is Global Namespace?

The global namespace in simple terms is any name defined in the main program that is referenced without additional qualifiers.

Now create another file called program.py and add the below statements

import mylib

a = 20
print(a)
print(mylib.a)

Output:
20
10

When print(a) is executed, there is only one 'a' variable in the global namespace. And when print(mylib.a) is executed, the program is referencing the variable 'a' that is within the mylib namespace and not the one in the global namespace. So in other words, to access members in mylib.py file, you have to use the prefix 'mylib' and the dot operator to fully qualify the namespace.

Now let us rearrange our statements in program.py as shown below:

from mylib import a
print(a)
a = 4;
print(a)

Now the name 'a' from mylib is imported into the global namespace of program.py. And this 'a' is reassigned a value of '4' and in effect we are only referencing one 'a' variable in this program.

Now change the mylib program by adding a print statement as given below:

a = 10
print(a)

Run program.py again. What do you see? Inside the mylib module, the value of 'a' is 10 when the print statement is executed. This is because each module has its own global namespace. The a in mylib.py belongs to the mylib module's namespace, and the a in program.py belongs to the program.py module's namespace.

What is a Scope?

In Python eBook, you were introduced to scope. In this lesson, we will dig a little deeper into this concept. In simple terms a scope is all the areas of the code where a particular variable is visible to the program.

When a particular variable is being referenced in a program, the search starts from the inner most scope and then starts stepping to one outer scope at a time until it reaches the global scope and then finally the built-in namespace.

Reference: https://docs.python.org/3/tutorial/classes.html

Privacy Policy | Terms & Conditions