> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Python Global Variable

A global variable in Python is a variable defined at the module level, outside of any function or class definition. Because Python does not implement block-level scoping for control flow statements, variables initialized inside `if`, `for`, `while`, or `with` blocks at the module level are also treated as global variables. Once initialized, a global variable is bound to the module's global namespace and can be referenced from any execution context within that same module.

Under the hood, Python stores global variables in a dictionary associated with the module, which can be inspected using the built-in `globals()` function. During variable resolution, Python follows the LEGB (Local, Enclosing, Global, Built-in) rule, checking the global namespace if a variable is not found in the local or enclosing scopes.

## Reading Global Variables

When a variable is referenced but not assigned a value within a local scope, Python automatically resolves it to the global namespace.

```python theme={"dark"}
x = 100  # Global variable

def read_global():
    # Python resolves 'x' in the global namespace (LEGB rule)
    print(x) 

read_global()  # Output: 100
```

## Variable Shadowing (Implicit Local Binding)

If an assignment operation (`=`) is performed on a variable name inside a local scope, Python's default behavior is to create a new local variable in that function's namespace. This local variable shadows the global variable of the same name for the duration of the function's execution. The global variable remains unmodified.

```python theme={"dark"}
y = 200  # Global variable

def shadow_global():
    y = 50  # Creates a new local variable 'y', shadowing the global 'y'
    print(y)

shadow_global()  # Output: 50
print(y)         # Output: 200 (Global remains unchanged)
```

## Lexical Scoping and `UnboundLocalError`

Python determines variable scope lexically at compile time. If a variable is assigned a value anywhere within a local scope, Python treats it as a local variable for that *entire* scope. Attempting to read the variable before the assignment occurs will not resolve to the global namespace; instead, it raises an `UnboundLocalError`.

```python theme={"dark"}
count = 10  # Global variable

def increment_counter():
    # Python sees 'count' is assigned later in this block, 
    # making it a local variable for the entire function.
    # Reading it here raises an UnboundLocalError.
    print(count) 
    count += 1   

increment_counter()  # Raises: UnboundLocalError: cannot access local variable 'count' where it is not associated with a value
```

## Modifying Global Variables: The `global` Keyword

To modify an existing global variable from within a local scope (and avoid `UnboundLocalError`), you must explicitly declare the variable using the `global` keyword before assigning a value to it. This directive instructs the Python interpreter to bypass local namespace binding and map the assignment directly to the module-level namespace.

```python theme={"dark"}
z = 300  # Global variable

def modify_global():
    global z     # Instructs interpreter to bind 'z' to the global namespace
    z += 50      # Modifies the global variable directly

modify_global()
print(z)         # Output: 350
```

## Cross-Module Global Variables

Python does not have true cross-file global variables by default. A global variable is strictly scoped to the module (file) in which it is defined. To access or modify a global variable from another module, the variable must be accessed as an attribute of the imported module object.

```python theme={"dark"}

# config.py
timeout = 60


# main.py
import config


# Accessing the global variable from config.py
print(config.timeout)  


# Modifying the global variable in config.py's namespace
config.timeout = 120   
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Python Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
