Python Scope

Python Scope

We need to define variables in every programming language before we use them. Variables in Python has some scope rules. These scope rules define the accessibility of a variable and what are the boundaries of it.

 

Python, unlike many programming languages, does not require you to define the type of the variable before initializing it to value and this makes the name of the variable bound to that data type.

Therefore, it is necessary to have some boundaries for the variables in the code. This also lets us control unexpected behaviour from our code.

Variable Scope

The scope of a variable in Python or programming in general means at what parts of the code, a variable can be used. 

Generally, there are two types of variables depending on their scopes in Python:

  • Global Variables
  • Local Variables

Global Variables

Global variables are created and initialized outside of a function and are not exclusive to a single function or a part of the code and hence can be accessed from any part, inside or outside of a function.

By saying that a variable is global, we mean that the scope of the variable is global.

Example

As you can see in the output above that both the values are the same and the function was able to access the variable “a” without any error.

Let us try changing the value of our variable inside the function –

Example

This code returns an error because Python is treating “a” as a global variable and when we tell Python to change the value of the variable inside a function, it tries to find that variable inside the function and fails. This takes us to our next topic.

Local Variables

Local variables can only be accessed by the function inside which they are initialized and declared. 

Example

We created a variable inside a function, but for now, it is acting like a normal global variable. Let us see what happens when you try to access outside of the function.

Example

We got an error when we tried to print the variable outside of the function because of the local scope of our variable.

You can use the local variables as global variables or vice versa using the global keyword.

global Keyword

This keyword is used whenever we want to change the scope of a variable. It can be used to make the local variable global or if we want to change the value of a global variable in a function.

Example

Remember earlier we were not able to change the value of a global variable inside a function, but with the help of the global keyword, it is possible.

Now, let us make a local keyword a global variable using this keyword.

Example

The function is only used to make the variable global and assign it. We are able to access the variable outside of the function in which it was created. This now makes the variable global.

Local and Global variables together

So, which variable a function will use if there are two variables with the same name, global or local? 

Example

As you may have already guessed, the function will use its own local variable instead of the global variable.

nonlocal Keyword

Let’s take an example of two functions nested together.

Example

The code prints the variable that was created in the function because the print statement in the code is written for the func() instead of func1().

But what if we want to print the variable created inside the nested function, well we can do this using the nonlocal keyword.

Example

The nonlocal keyword made the Python interpreter point to the variable of the nested function instead of the main function.

Tips
It is your code and it depends on you how you want to write and if you are one of those programmers who like to use the same name for multiple variables, these keywords are important to you.

Tutorials for all brains!