Variable Scope Concepts in Python: Important Concepts and Differences

Introduction: Variable Scope Concepts in Python

Some variable scope concepts in Python apply to more than one topic, such as local variables, global variables, and the global keyword. These shared concepts are collected on this page so the same explanation does not need to be repeated across multiple tutorials.

  1. Local and Global Variables With the Same Name
  2. Local vs Global Scope in Python
  3. Accessing Variables From Different Scopes
  4. nonlocal vs global
  5. Key Takeaways: Python variable scopes

Local and Global Variables With the Same Name

It is one of the important variable scope concepts in Python. This concept often confuses beginners when a local variable and a global variable have the same name. Knowing how Python handles this situation helps avoid unexpected results when working with variables inside and outside functions. Let’s explore this concept in detail.

Rule: When this happens, the local variable takes priority inside the function.

message = "Global"

def show_message():
    message = "Local"
    print(message)

show_message()
print(message)


# Output:
Local
Global

Explanation: Inside show_message(), Python uses the local message variable. Outside the function, Python uses the global message variable.

The local variable does not change the value of the global variable simply because both variables have the same name.

↑ Move to Top

Local vs Global Scope in Python

A clear understanding of the difference between local and global scope is essential for mastering variable scope concepts in Python. Let’s see where variables in each scope can be accessed.

Feature Local Scope Global Scope
Where the Variable Is Created Inside a function Outside functions
Where It Can Be Accessed Inside the function where it is defined From other parts of the program when available
Accessed Inside a Function Yes Yes
Accessed Outside the Function No Yes
Lifetime Generally exists while the function is executing Generally exists while the program is running
Needs global When Assigned Inside a Function No Yes, when modifying the global variable from inside a function

↑ Move to Top

Accessing Variables From Different Scopes

Knowing where a variable can be accessed is an important part of variable scope concepts in Python. A function can access a global variable, while a local variable is available only within that function. Let’s understand this with an example.

language = "Python"

def show_language():
    version = "3.14"
    print(language)
    print(version)

show_language()


# Output:
Python
3.14
Explanation:
  • language is a global variable, so show_language() can access its value.
  • version is a local variable, so it can be accessed only inside show_language().
  • Code outside show_language() cannot directly access the local version variable.

Let’s see what happens when a local variable is accessed outside its function.

Accessing a Local Variable Outside Its Function

What happens if we try to access a local variable outside its function?

def show_version():
    version = "3.14"
    print(version)

show_version()
print(version)


# Error:
NameError
Explanation:
  • The version variable is created inside show_version(), so it is a local variable.
  • It can be accessed inside the show_version() function.
  • Trying to access the version variable outside the function raises a NameError.

↑ Move to Top

nonlocal vs global

Both nonlocal and global are important variable scope concepts in Python that allow a function to modify a variable outside its local scope. However, they refer to different scopes.

Feature global nonlocal
Refers To The global scope The nearest enclosing function’s scope
Used Inside A function A nested function
Variable Must Exist In The global scope An enclosing function scope
Can Modify a Global Variable Yes No
Can Modify an Enclosing Function Variable No Yes
Works Across Nested Function Scopes Yes, for a global variable Yes, for a variable in an enclosing function

Let’s see how global affects variables in different scopes with an example.

Example

count = 0

def outer():
    count = 10

    def change():
        global count
        count = 20

    change()
    print(count)

outer()
print(count)


# Output:
10
20

Explanation:

  • global count refers to the global count, so its value changes from 0 to 20.
  • The count inside outer() remains 10 because it is a separate local variable.
  • This shows that global modifies the global variable, not the variable belonging to the enclosing function.

↑ Move to Top

Key Takeaways: Python variable scopes

Here are the main points to keep in mind when working with variable scopes in Python and related keywords.

  • A local variable and a global variable can have the same name.
  • When the same name exists in local and global scopes, the local variable is used inside the function.
  • Local and global scope have different access boundaries.
  • The global keyword refers to the global scope.
  • The nonlocal keyword refers to a variable in an enclosing function scope.
  • Shared scope concepts are kept on this page to avoid unnecessary duplication across the dedicated tutorials.

↑ Move to Top

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top