CommonLounge Archive

Python 3: Functions and Scope

May 23, 2019

In the earlier tutorial on functions, you created functions with parameters. In this tutorial, let’s learn more about scope of the variables and how they work with functions. Let’s get started.

Scope of variables

Scope defines the visibility or accessibility of the variables defined in the program. In simple words, scope is the container for variables and it determines which section of the code can or can’t access the variable.

There are two types of variables based on scopes:

  • Global Variables
  • Local Variables

Global Variables

These are the variables that are defined outside all the functions. Hence they are accessible within all the functions.

The values assigned in the global variable is accessible throughout the program run time and they are destroyed when the program is terminated.

Let’s look at an example below to see the scope of global variable

## Declaring the variable name outside the function ##
name = 'Commonlounge'
def printname():
    ## Accessing the variable inside the function ##
    print ("Accessing inside the function: ",name)
## Accessing variable outside the function ##
print ("Accessing outside the function: ",name)
printname()

The output will be

Accessing outside the function: Commonlounge
Accessing inside the function: Commonlounge

If you notice here, we have defined the variable name outside the function printname. Thus, the function is able to access the variable even if it doesn’t take the variable as a parameter.

Local Variables

Any variables which are defined and assigned within the function are known as local variables. These variables are not accessible outside the function.

The value assigned inside the function is available only till the return statement is executed within the function. Every new call for the function results in new assignment of local variables.

def printname():
    ## Declaring the variable name inside the function ##
    name = 'Commonlounge'
    ## Accessing the variable inside the function ##
    print ("Accessing inside the function: ",name)
printname()

The output will be as follows:

Accessing inside the function: Commonlounge

Let’s test your understanding so far on the scope of variables

Accessing Local Variables in Global Scope

Let’s look at a slightly modified version of previous example to understand what would happen if we access the local variable in a global scope:

def printname():
    ## Declaring the variable name inside the function ##
    name = 'Commonlounge'
    ## Accessing the variable inside the function ##
    print ("Accessing inside the function: ",name)
printname()
## Accessing variable outside the function ##
print ("Accessing outside the function: ",name)

The output will be:

Accessing inside the function: Commonlounge
NameError: name 'name' is not defined

Notice that the second print statement fails with NameError because the variable name is accessible only within the function scope. Thus, a locally defined variable cannot be used in global scope outside the function.

Accessing Local Variable in another local scope

As we learned earlier, the local variables are available only within the local scope. Let’s look at another example to see what happens when we access a local variable of one function block in another.

def foo():
    a = 10
    print ("Inside foo :", a)
def bar():
    print ("Inside bar :", a)
foo()
bar()

The output will be

Inside foo : 10
NameError: name 'a' is not defined

The print statement in function foo will be executed successfully while the same in function bar will fail with Namerror. This is because, the scope of local variables are limited only to the function block where it is declared. Hence, they can’t be accessed from another function block’s local scope.

Accessing Local Variable of same name as Global Variable

As we noticed in previous section, the variables are accessible in local scope. But if there is another variable declared with same name in the local scope, then the local variable takes precedence over the global variable.

Lets look at the example:

a = 5
def foo():
    a = 10
    print ("Inside foo :", a)
def bar():
    print ("Inside bar :", a)
foo()
bar()

The output will be

Inside foo : 10
Inside bar : 5

Here the function foo will print the value assigned locally while the function bar will access the value from global scope.

Global Keyword

In the previous example, we saw that the local scope variable takes precedence over the global variable. But what if you want to reassign the global value so that the other functions that access the global variable can also get the new values?

Let’s look at the previous example with a small modification:

def foo():
    global a
    a = 10
    print ("Inside foo :", a)
def bar():
    print ("Inside bar :", a)
foo()
bar()

The output will be

Inside foo : 10
Inside bar : 10

Hurray! Did you notice, the simple addition of global keyword to the variable declaration helped us declare a as a global variable, which was accessible in the other bar() function as well. The global keyword is a special way to declare a global variable in the function block.

Let’s check if you have understood the concept well.

Summary

To recap, we learned the following in this tutorial:

  • Scope of variables.
  • Global vs local variables.
  • Local Variables Cannot Be Used in the Global Scope.
  • Local Scopes Cannot Use Variables in Other Local Scopes.
  • Global Variables Can Be Read from a Local Scope.
  • Local and Global Variables with the Same Name.
  • The global Statement.

© 2016-2022. All rights reserved.