What is Namespace and Variable Scope in Python - Programmer's Academy

What is Namespace and Variable Scope in Python

Introduction to Namespace

A namespace is basically a class of different elements. These elements can be used for different purposes by different classes and objects. In programming, namespaces are used to organize the names assigned to objects. Everything in modern programming objects so to keep them organized namespaces exist. Namespace has 2 components let us say that name and space. In x=2,2 is space and x is the name assigned. That is how namespace works. Its most basic function is to avoid ambiguity.

Namespace in Python

Similarly, in python namespace is a collection of built-in name, functions and exceptions which are structured inside python interpreter. It has two components i.e., key and value. For example Print() is a built-in function you use it directly because its value lies inside namespace.

Python has 3 main types of namespaces.

Built-in Namespace

Built-in namespace is the one that is located inside python interpreter by default. It consists of names of almost every object in python, such as Print, float, int, max, min, object, errorIO, str, sum etc. Built-in namespaces in python are created right with the startup of interpreter and terminated when program is terminated.

To see all objects in built-in namespace

  • Go to Command prompt
  • Type python
  • Then type “dir(__builtins__)”

Global Namespace

Global namespace belongs to main program. It holds the names and functions that are being used in different modules of the program. It contains the names that are define at main function level. Global namespace is created right after main program. It can be terminated by interpreter.

Local Namespace

When a function is executed a new namespace is created by python interpreter every time. That namespace is local namespace. local namespace exits until the existence of function and terminated after that by the interpreter.

Scope

Scope is the lifetime of a variable, function, constant etc. It defines boundaries for variable, functions, constants in which they will exist and can be used or referred to. Remember that program is constricted in structured way and not all variable, functions can be accessed through all parts of program. Scope restricts the visibility. To understand, we will use variable scope for understanding.

There are 4 types of scope in python.

Local scope

In local space, variable or function cannot be accessed outside the function/class in which it Is declared. It scopes only lies within that boundary. Nothing from outside can change its value. Its lifetime is till the function is in execution. For example

First number variable was declared inside function so its scope lies inside that function printlocal() when accessed outside the namerror was thrown as shown in output.

Try it yourself using first number = 12.5!

Enclosing space

Enclosing space comes in play when we have got nested function i.e., function inside a function. Suppose we have two functions, function1 and function2, they are nested means function2 lies within function1. Function2 can access the variable defined in function1 but function1 cannot access the function that is defined in function2 that is inner or nested function. 

Example

Global Scope

Global scope holds variables and functions which can be used anywhere in the program. This is free to use scope and most simple and easily understandable scope.

Recall last example after adding global variable it gives results.

 

Built-in scope

Built-in scope is wider than global and can accommodate many functions. Remember in previous topic we learned about built-in namespace. Whole built-in namespace lies in built-0in scope that means all functions from built-in namespace can be used. All those keywords/names can be used directly without declaration.

Help Me Please: If you found any error in the content or example please let us know by comment also write your suggestion or if any doubt in the concept please feel free to write us in the comment section.

Practice your Code online here

(Visited 90 times, 1 visits today)

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
//poosoahe.com/4/4219743
You cannot copy content of this page
Scroll Up
0
Would love your thoughts, please comment.x
()
x

  • Name

  • Minimum length of 8 characters.