Python Scope Rules: Local and Global Variables (With Examples)

python-scope-rules-with-local-and-global-variables-with-examples

In Python, the scope of a variable refers to the region of the program where the variable is accessible.

Python functions have their own scope, which means that variables defined within a function are not accessible outside of the function.

Types of Python Scopes

There are two types of scope in Python: global scope and local scope.

  1. Global Scope
  2. Local Scope

Global Scope

Variables defined outside of any function have global scope, which means they can be accessed from anywhere in the program, including inside functions.

However, if a function defines a variable with the same name as a global variable then the function will use its own local variable instead of the global variable.

x = 10

def my_function():
    print(x)

my_function()

Output

10

Declaring a variable of Global Scope inside a function with the global keyword

global keyword is a built-in keyword in python that you can use to declare a global variable or to change the value of a global variable.

Syntax

x = 2

def my_function():
    #first call the global variable with global keyword
    global x
    #then modify its value
    x = 8

One thing to keep in mind here is that you must use global keyword followed by the variable name.

And on the next line, you can assign the value. Let’s have a look at another example:

x = 10
z = 2

def my_function():
    global y
    y = 2
    global z
    z = 4
    print("X =", x)

my_function()
print("Y (Declared in my_function and accessed globaly) = ", y)
print("Z (Declared globally but changed in my_function) = ", z)

Output

X = 10
Y (Declared in my_function and accessed globaly) =  2
Z (Declared globally but changed in my_function) =  4

In the example above, you can see, a new variable y is declared as a global variable inside my_function.

At the same time, the value of the global variable z is changed in the function my_function.

Local Scope

In Python, a local scope is created whenever a function is called.

A local scope is a temporary workspace where variables and objects created within a function exist.

These variables can only be accessed within the function and are destroyed when the function completes execution.

Here is an example that illustrates the concept of local scope in Python:

def my_func():
    x = 10
    print("The value of x inside my_func is:", x)

my_func()

Output

The value of x inside my_func is: 10

But what if we try to access variable ‘x’ outside the function? Let’s check that.

def my_func():
    x = 10
    print("The value of x inside my_func is:", x)

my_func()
print("Trying to access X outside the function", x)

Output

The value of x inside my_func is: 10
Traceback (most recent call last):
  File "<string>", line 6, in <module>
NameError: name 'x' is not defined

Why this error popped up?

You can not access ‘x’ outside the function. Because variable x has local scope and you can only access this variable within my_func().

That’s how local scope works.

If we want to use the value of x outside the function, we need to return it from the function and assign it to a variable in the global scope:

def my_func():
    x = 10
    return x

result = my_func()
print("The value of x outside my_func is:", result)

Output

The value of x outside my_func is: 10

In this example, we assign the returned value of 10 from my_func to the variable result in the global scope.

Now we can print the value of result outside the function, which outputs ‘The value of x outside my_func is: 10‘.

Was this helpful?
YesNo

Related Articles:

Recent Articles:

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x