Understanding Local vs Global Variables

First off, let’s clear up some confusion: both locals and globals are dictionaries in Python. The main difference is where they live. Locals live inside your functions, while globals live outside of them (in the module scope).

Now, you might be thinking to yourself, “But wait a minute! I thought variables were just names that point to values? How can locals and globals both be dictionaries?” Well, my friend, you’re not wrong. But in Python, variables are actually references to objects (or None if they don’t have an object assigned yet). When we assign a value to a variable, what we’re really doing is creating a new key-value pair in the dictionary that corresponds to that variable name.

So let’s say you have this code:

# This function is defined with the name "my_function"
def my_function():
    # A variable "x" is created and assigned the value of 5
    x = 5
    # The value of "x" is printed
    print(x)
    
# The function "my_function" is called
my_function()
# This line will throw an error because "x" is not defined outside of the function
print(x) 

# To fix the error, we can either define "x" outside of the function or return the value of "x" from the function and assign it to a variable outside of the function. 
# Here is an example of defining "x" outside of the function:
x = 5

def my_function():
    # The value of "x" is printed
    print(x)
    
my_function()
# The value of "x" is still accessible outside of the function
print(x)

When `my_function` is called, a new dictionary (locals) is created for that function. Inside this locals dictionary, we create a key-value pair with the name ‘x’ and the value 5. When the print statement inside the function executes, it prints out the value of x from the locals dictionary.

But what happens when you try to access `x` outside of the function? Well, since there is no locals dictionary for that scope (i.e., outside of the function), Python looks in the globals dictionary instead. But since we haven’t defined a global variable named ‘x’, it throws an error!

So what if you want to access or modify variables from both inside and outside of your functions? That’s where the `global` keyword comes in handy:

# This script demonstrates the use of the `global` keyword in Python to access and modify variables from both inside and outside of functions.

# Define a global variable named 'x'
x = 5

# Define a function named 'my_function'
def my_function():
    global x # Use the `global` keyword to access the global variable 'x' within the function
    x = 10 # Modify the value of 'x' to 10
    
    # Do some stuff with x...
    print("Inside the function, x =", x) # Print the value of 'x' inside the function

# Call the function
my_function()

# Print the value of 'x' outside the function
print("Outside the function, x =", x) # The value of 'x' has been modified to 10 inside the function and this change is reflected outside the function as well.

By using the `global` keyword, we’re telling Python to look for ‘x’ in the globals dictionary instead of creating a new locals variable. And since we defined ‘x’ outside of the function (in the global scope), it can access and modify that value without any issues!

But be careful with using the `global` keyword, as it can lead to some pretty confusing code if you’re not careful. It’s generally best practice to avoid modifying globals unless absolutely necessary. Instead, try passing in arguments or returning values from your functions whenever possible.

SICORPS