Python - Scope of Variables

Python

Share
python logo

In this tutorial we will learn about scope of variables in Python.

What is scope of a variable?

The variable we create in a Python program are not accessible everywhere. They have a scope which defines were we can access them.

There are two categories of variable scope.

  • Global variable scope
  • Local variable scope

What is a global variable?

Variables that are defined outside of any function are referred as global variables.

Global variables are accessible both inside and outside of a given function.

In the following Python program we are creating a global variable and accessing it both inside and outside a function.

# global variable
score = 10

print("global variable score:", score)

# func
def foo():
    print("value of score from inside foo():", score)

# calling foo
foo()

The above code will give us the following output.

global variable score: 10
value of score from inside foo(): 10

What is a local variable?

A variable created inside a function is referred as local variable.

We can't access local variable outside a function.

In the following Python program we are creating a local variable inside a function.

# func
def foo():
    x = 10
    print("local variable x:", x)

# function call
foo()

The above code will give the following output.

local variable x: 10

Trying to access local variable outside the function will raise an error.

Mask global variable inside function

We can mask global variable inside a function by creating a variable by the same name.

In the following Python program we are creating a global variable and then masking it inside a function by creating a local variable by the same name inside the function.

# global variable
x = 10
print("global variable x before function call:", x)

# func
def foo():
    # local variable
    x = 20
    print("local variable x inside foo():", x)

print("function call")
foo()

print("global variable x after function call:", x)

The above code will print the following output.

global variable x before function call: 10
function call
local variable x inside foo(): 20
global variable x after function call: 10

Change global variable from inside a function

As we saw earlier that if a variable inside a function shares the same name as a global variable then the local variable masks the global variable.

So, any change made to the local variable is not reflected back to the global variable as it is masked.

To modify the global variable from inside a function we have to use the global keyword and then work with the global variable.

In the following Python program we are changing the value of a global variable from inside a function.

# global variable
x = 10
print("global variable x before function call:", x)

# func
def foo():
    # get the global variable
    global x

    # now change the value
    x = 20
    print("global variable x inside foo():", x)

print("function call")
foo()

print("global variable x after function call:", x)

The above code will give us the following output.

global variable x before function call: 10
function call
global variable x inside foo(): 20
global variable x after function call: 20
Share