Kahibaro
Discord Login Register

Scope (local vs global)

Understanding Scope in Python

In Python, scope determines where in your code a variable can be seen (accessed) and used. In this chapter, you’ll focus on two main kinds of scope:

You’ll also see how scope works with functions, and some common pitfalls to avoid.


Local Scope: Variables Inside Functions

A local variable is a variable that is created inside a function. It:

Example:

def greet():
    message = "Hello!"   # local variable
    print(message)
greet()
print(message)  # This will cause an error

Explanation:

You can think of a function as its own “room”; variables created in that room can’t be seen from outside.


Global Scope: Variables Outside Functions

A global variable is a variable that is created outside of all functions (at the top level of a file). It:

Example:

message = "Hello from global scope!"  # global variable
def show_message():
    print(message)   # we can read the global variable
show_message()
print(message)

Here:

Local vs Global with the Same Name

If a variable with the same name exists both globally and locally, Python will use the local one inside the function.

Example:

x = 10   # global variable
def test():
    x = 5   # local variable
    print("Inside function:", x)
test()
print("Outside function:", x)

Output:

Inside function: 5
Outside function: 10

This is called variable shadowing: the local x shadows (hides) the global x inside the function.


Reading Global Variables Inside Functions

You can always read a global variable inside a function (as long as you don’t try to assign to a variable with the same name in that function).

Example:

price = 100
def show_price():
    # Only reading price, not assigning to it
    print("The price is", price)
show_price()

This works fine because the function is only reading price, not creating or changing a local variable with the same name.


Why Assignment Changes Things

If you assign to a variable name inside a function, Python treats that name as local to that function (unless you explicitly tell Python otherwise). That means you can’t both assign and read the global variable under the same name without extra steps.

Example that causes an error:

count = 0
def increase():
    print(count)   # trying to read count
    count = count + 1  # assigning to count (local)
increase()

This will cause an UnboundLocalError. Why?

To fix this, you can:

  1. Use a different local variable name, or
  2. Pass count as a parameter and return a new value, or
  3. Use the global keyword (explained next).

The `global` Keyword (Changing Global Variables)

If you really need to change a global variable from inside a function, you can use the global keyword.

Syntax inside a function:

global variable_name

This tells Python:

“When I use variable_name in this function, I mean the global one, not a new local one.”

Example:

counter = 0   # global
def increase():
    global counter   # use the global counter, not a new local one
    counter = counter + 1
increase()
increase()
print(counter)   # 2

Now counter is updated globally.

When (and When Not) to Use `global`

Using global can make code harder to understand and debug, because many different parts of your program can change the same variable.

Better approaches:

Example without global:

def increase(value):
    return value + 1
counter = 0
counter = increase(counter)
counter = increase(counter)
print(counter)   # 2

This is clearer: increase does not depend on any global state.


The `nonlocal` Keyword (Mention Only)

In nested functions (a function defined inside another function), there is another keyword: nonlocal. It lets you change a variable from an outer function’s scope (not global, not local to the inner function).

You’ll rarely need this as a beginner, but here’s a tiny example so you recognize it:

def outer():
    x = 10
    def inner():
        nonlocal x
        x = x + 1
        print("Inside inner:", x)
    inner()
    print("Inside outer:", x)
outer()

Here, nonlocal x tells Python that x comes from outer(), not from inner() and not from the global scope.


The LEGB Rule (Where Python Looks for Variables)

When Python sees a variable name, it checks scopes in this order (LEGB):

  1. Local – inside the current function.
  2. Enclosing – in any outer functions (for nested functions).
  3. Global – at the top level of the file.
  4. Built-in – names that Python provides (like len, print).

It stops as soon as it finds the name.

Simple example (without nesting):

x = 100  # global
def demo():
    x = 5  # local
    print(x)
demo()        # prints 5 (local)
print(x)      # prints 100 (global)

Python:

Scope and Function Parameters

Function parameters behave like local variables inside the function.

Example:

tax_rate = 0.2  # global
def calculate_price(price):  # price is a local variable
    total = price * (1 + tax_rate)  # total is also local
    return total
final = calculate_price(50)
print(final)
print(price)   # error: price is local to the function

Common Scope Mistakes and How to Avoid Them

1. Expecting Local Variables to Be Global

Mistake:

def create_name():
    name = "Alice"
create_name()
print(name)  # error

Fix: return the value and assign it outside:

def create_name():
    return "Alice"
name = create_name()
print(name)

2. Overusing Global Variables

Mistake:

total = 0
def add_to_total(x):
    global total
    total += x
add_to_total(5)
add_to_total(10)
print(total)

This works, but logic is hidden and spread out.

Better: use return values:

def add(a, b):
    return a + b
total = 0
total = add(total, 5)
total = add(total, 10)
print(total)

3. Shadowing Global Names Without Realizing

Mistake:

data = [1, 2, 3]
def process():
    data = "not a list anymore"  # shadows global data
    print(data)
process()
print(data)  # still the original list, not changed

If you meant to change the global data, you must either:

Practical Guidelines for Using Scope

By understanding scope, you can write functions that are easier to read, test, and reuse, without unexpected side effects from variables changing in surprising places.

Views: 16

Comments

Please login to add a comment.

Don't have an account? Register now!