namespace in python and variable scope with example

Namespace in python and variable scope

namespace in python and variable scope with example:– what are names in Python why we use names in Python so we’ll use names in the program to identify an object right in the real-life will use the name to identify a person in the program we’ll use names to identify the object so that’s why they are called as identifiers?

what kind of names we use in our program variable name function name class the method name right these are all the identifiers so we know what is name means.



what is namespace in python:

so namespace in python is basically a system that will control all the names which we use in our program. it will assure that whatever the names we use is unique and it won’t lead to any conflict so that’s nothing but in the symbol words the namespaces in python is the system that will control all the names in our program and it will allow us to reuse a name in your program.

so we’ll take the example so first of all why I want to reuse any NAME and what will happen if I reuse a name so here I will take a variable name x

X is a variable 4 is the value now if I access the value of x it will give four

so now in the next line what I will do is I will take X as 5 I will assign a new value to the variable X I’ll use this same name now if I enter X and see I’ll get the value

5 and I lost the previous value 4 now I can’t access the value 4 using variable X so when we reuse a name we may be lost some data that means we can’t access that data and we can’t access 4  using X and also using any other variable because previously 4 was stored in variable X now a new value is stored in that variable so we can’t access 4.


fine I won’t reuse the name I’ll look you to a different name like

and I’ll give

now I can access X also Y also

I can get 4 also 5 also.

then what is the work of namespace in python?  I’ll make sure that whatever the names I use are unique I’ll take care of that then why we need namespace in python

 that is because if you are writing a 10 or 20 line code then that’s you can take care of the variable names and you can make sure that whatever the names you used are unique alright but if you are writing a long program and you are using external functions also like you are importing the models then in that model you don’t know which names you used you maybe use the names in the different modules in another case half of the code is written by your friend and half is written by you then you don’t know which names he used or she used in her part of code right so you maybe use that same names so in that case you can’t sit and check all the names right in that cases namespace comes to play and it will allow us to reuse the names and a Python program contains multiple python namespace.


Type of  namespaces in python :

there are different types of namespaces  in python which are:

built-in namespace

global namespace

local namespace

built-in namespace:

when you start the Python interpreter built-in namespace is created. it contains all the built-in names

global namespace:

when you import any module and you use any module that time it will create its own global namespace so each module has its own namespace that’s why you can reuse names in different modules

local namespace:

when you call a function it will create its own namespace that is called a local namespace



Realistic example of namespace in python:

we’ll take an realistic example so first is like in the program I told you if you are writing 20 30 line code you are not importing any model then you can’t take care about the names right you can’t take unique names and you can write the program there will be no name collusion for this realistic example is in your family your parents and relatives won’t repeat any name like my name is fawad khan so my parents and my relatives own name any of my cousin as fawad  they’ll search for the unique name different name right so in our family we can’t take care about that there will be no name collusion but if we are writing a long program and we are importing some models and all then we can’t keep the eye on the uniqueness of the names right for that realistic example is you will go to the College and there many person can have same names right then how will you differentiate them using their surnames right I’m fawad khan  some other student may have same name fawad but different surname like ali  so using that surname we can identify them. in programming also we’ll do the same thing here when we have different names we’ll identify them through namespace in the module can see when we import module like any module name

we will call the function as

so that’s nothing but we are telling the Python that this function belongs to this module. So each module has its own namespace in python even though they have the same name but module name is different that’s why they can be reused this way names can be reused.

for this I’ll take an example now here I’ll show you how the namespace works in programming:

first.py

second.py

main.py

now in the main program, I’ll import both the module import first and import second

here while calling the function data how I will call first that is module name dot function same for the second function, second dot function as you can see

even the function name is same but the module name is different we are calling the function using module name also that’s like if two-person has the same name we’ll differentiate them using their surname right we’ll mention their complete name that’s what we are doing here

that’s why there will no collision. so this is about namespace so namespace in Python will take care of the names and it will allow us to use the name in a different namespace.


Namespace in Python Program:

output:

namespace in python


Variable scope in python:

as I said we can use any names in the program but we can’t access any name from the any part of the program.

so for example I will define a function

the function name is f1 inside this I’ll take X is equal to 10 and print X.

so next I will call that function it will print 10

namespace in python

now what I’ll do is I’ll try to access the value of x it will give error name error why because X is defined inside the function

namespace in python

it can be accessible only inside the function we can’t access it outside the function it has its own lifetime it has its own region where only it is available. so this is called scope variable,  variable scope is the part of the program where the variable is accessible is called variable scope.

there are  4 different types in the variable scope which are

 local scope variable

 global scope variable

 enclosed  scope variable

built-in scope variable

I’ll discuss these types in the next article okay we’ll discuss this in the LEGB rule okay so, for now, don’t worry about it.

Recommended For You

About the Author: Fawad

My name is Shahzada Fawad and I am a Programmer. Currently, I am running my own YouTube channel "Expertstech", and managing this Website. My Hobbies are * Watching Movies * Music * Photography * Travelling * gaming and so on...

Leave a Reply

%d bloggers like this: