Namespace and scope in Python

Namespace and scope in Python are very much interrelated. In this section, we will take a look at this very important concept.

Before giving an example we can take a look at the file systems in a computer.

Suppose in C drive, we try to save a file under the same name. Will it allow us to do that? 

No.

Either it will ask to overwrite the existing file, or add a number with it to distinguish between two files. Right? 

In Python things happen in a different way. 

When we declare a variable or a function, which are objects in Python, we declare them in the global namespace.  



If you are a complete beginner your journey to learn TensorFlow might start from here.

For the TensorFlow beginners we have a dedicated category – TensorFlow for Beginners.
But besides that, you may need to learn several other machine learning and data science libraries.

As a result, you may check these categories as well – NumPy, Pandas, Matplotlib.

However, without learning Python, you cannot learn the usages of these libraries. Why? Because they all use Python as the Programming language.

Therefore please learn Python at the very beginning and start learning TensorFlow.

And, finally please check our Mathematics, Discrete Mathematics and Data Structures categories specially. We have tried to discuss from basic to intermediate level so that you can pick up the core ideas of TensorFlow.


However, in the next line, we can use the same name.

However, when we call that variable it displays the value of the second variable.

It acts like a PC because in essence the second variable in the same name overwrites the first one.

Let’s see an example to understand this concept.

a = 10

def display_a_number():
  a = 100
  print(f"Local a : {a}")

print(f"Global a : {a}")
display_a_number()

Here goes the output when we run the code.

Global a : 10
Local a : 100

In the above example, we have used the same variable a. 

However, we declare one variable in the global namespace. And we declare another variable in the local namespace.

Although both the variables have the same names, that is ‘a’, the local one remains inside the function which is global, of course.

Let us try some more examples.

# namespace and scope

a_in_global_namespace = 100

def display_in_global_namespace():
  a_in_local_namespace = 10
  print(f"Inside in global namespace: {a_in_global_namespace}")
  print(f"Inside in local namespace: {a_in_local_namespace}")
  
  def display_in_local_namespace():
    a_in_nested_local_namespace = 20
    print(f"Inside in global namespace: {a_in_global_namespace}")
    print(f"Inside in local namespace: {a_in_local_namespace}")
    print(f"Inside in local namespace: {a_in_nested_local_namespace}")

  display_in_local_namespace()



display_in_global_namespace()

Now we have given the unique names so that there should not be any confusion anymore.

Run the code, and we get the output as below.

Inside in global namespace: 100
Inside in local namespace: 10
Inside in global namespace: 100
Inside in local namespace: 10
Inside in local namespace: 20

In the next example we try the same-name-variable twice and see what happens.

a_in_global_namespace = 100

a_in_global_namespace = 200

def display_in_global_namespace():
  a_in_local_namespace = 10
  a_in_local_namespace = 100
  print(f"Inside in global namespace: {a_in_global_namespace}")
  print(f"Inside in local namespace: {a_in_local_namespace}")
  
  def display_in_local_namespace():
    a_in_nested_local_namespace = 20
    a_in_nested_local_namespace = 2
    print(f"Inside in global namespace: {a_in_global_namespace}")
    print(f"Inside in local namespace: {a_in_local_namespace}")
    print(f"Inside in local namespace: {a_in_nested_local_namespace}")

  display_in_local_namespace()



display_in_global_namespace()

# output
Inside in global namespace: 200
Inside in local namespace: 100
Inside in global namespace: 200
Inside in local namespace: 100
Inside in local namespace: 2

For more such Python code, please visit the respective GitHub Repository.

Let us come back to the personal computer analogy.

We can certainly use the same name for different files when we keep them in different directories. Right? 

As a result, we can locate the file through the directories’ absolute path name.

In Python every object has two spaces: global and local. 

For that reason, we identify the identifiers with a unique name.

Inside a function, the variable object’s scope is local.

But outside, it is in a global namespace. 

What Next?

Books at Leanpub

Books in Apress

My books at Amazon

GitHub repository

TensorFlow, Machine Learning, AI and Data Science

Flutter, Dart and Algorithm

C, C++, Java and Game Development

Twitter


Posted

in

,

by

Comments

Leave a Reply