First off, let’s clear up some confusion: there is no such thing as a “global variable” in Python. There are only globals and locals. But we’ll get to that later.
So what exactly do these terms mean? Well, imagine you have a function called `my_function()` that looks like this:
# This script defines a function called my_function()
def my_function():
# Within the function, a variable x is assigned the value of 10
x = 10
# The value of x is then printed
print(x)
# A nested function called inner_func() is defined within my_function()
def inner_func():
# Within inner_func(), a variable y is assigned the value of 20
y = 20
# The value of y is then printed
print(y)
# The nested function inner_func() is called within my_function()
inner_func()
# The value of x is printed again
print(x)
# The my_function() is called, executing the code within it
my_function()
# Output:
# 10
# 20
# 10
# This script defines a function called my_function()
def my_function():
# Within the function, a variable x is assigned the value of 10
x = 10
# The value of x is then printed
print(x)
# A nested function called inner_func() is defined within my_function()
def inner_func():
# Within inner_func(), a variable y is assigned the value of 20
y = 20
# The value of y is then printed
print(y)
# The nested function inner_func() is called within my_function()
inner_func()
# The value of x is printed again
print(x)
# The my_function() is called, executing the code within it
my_function()
# Output:
# 10
# 20
# 10
# Explanation:
# The script defines a function called my_function() which contains a nested function called inner_func(). The variable x is assigned the value of 10 within my_function() and is printed. Then, inner_func() is called and within it, the variable y is assigned the value of 20 and is printed. Finally, the value of x is printed again. When my_function() is called, the code within it is executed, resulting in the output of 10, 20, and 10.
When you run this code, the output will be:
# This script prints out the numbers 10, 20, and 10 in separate lines.
# The print() function is used to output the given values.
print(10) # Prints the number 10
print(20) # Prints the number 20
print(10) # Prints the number 10
But what’s going on here? How is `x` still accessible outside of its function scope?
Well, that’s because it’s a global variable. Wait, what?! Nope, sorry to disappoint you, but there are no such things as “global variables” in Python. Instead, we have globals and locals. Let me explain:
When you define `x` outside of any function (like at the top level of your script), it’s a global variable. This means that its value can be accessed from anywhere within your code. But when you define `x` inside a function, like in our example above, it’s actually a local variable.
So what happens when we call the inner function? A new set of locals is created for that specific function call. This means that `y` is only accessible within the scope of the inner function. But since `x` was defined outside of any functions, it’s still a global variable and can be accessed from anywhere in our code.
If you define another function called `my_other_function()`, like this:
# Defining a function called my_other_function
def my_other_function():
# Defining a local variable x and assigning it a value of 30
x = 30
# Printing the value of x
print(x)
# Defining a nested function called inner_func
def inner_func():
# Defining a local variable y and assigning it a value of 40
y = 40
# Printing the value of y
print(y)
# Calling the inner_func function
inner_func()
# Printing the value of x again
print(x)
# Calling the my_other_function function
my_other_function()
# Output:
# 30
# 40
# 30
# Explanation:
# The script defines a function called my_other_function, which contains a local variable x with a value of 30.
# It then defines a nested function called inner_func, which contains a local variable y with a value of 40.
# The inner_func function is called within the my_other_function function, and the values of x and y are printed.
# Finally, the value of x is printed again.
# Since x was defined outside of any functions, it is a global variable and can be accessed from anywhere in the code.
# However, y is only accessible within the scope of the inner_func function.
You might think that `x` is now a global variable, but it’s not! This is because Python keeps track of the variables defined within each function separately. So when you call `my_other_function()`, a new set of locals is created for that specific function call.
But what if we want to access a global variable from inside another function? Well, there are two ways to do this: using the `global` keyword or passing it as an argument. Let’s take a look at both options:
# Define a global variable x with value 10
x = 10
# Define a function named my_function
def my_function():
# Print the value of x, which is a global variable
print(x)
# Define a nested function named inner_func
def inner_func():
# Use the global keyword to access and modify the global variable x
global x
# Increment the value of x by 5
x += 5
# Print the updated value of x
print(x)
# Call the nested function inner_func
inner_func()
# Print the final value of x, which has been modified by inner_func
print(x)
# Call the function my_function
my_function()
# Output:
# 10
# 15
# 15
# Explanation:
# The global keyword allows us to access and modify global variables from within a function.
# Without using the global keyword, the variable x would be treated as a local variable within inner_func and any changes made to it would not affect the global variable.
# By using the global keyword, we are able to modify the global variable x from within the nested function inner_func.
# This is useful when we want to modify a global variable from within a function without passing it as an argument.
# However, it is generally considered better practice to pass variables as arguments rather than using the global keyword.
In this example, we’re using the `global` keyword to modify a variable that was defined outside of any functions. This is not recommended because it can lead to unexpected behavior and make your code harder to read and understand.
A better approach would be to pass the global variable as an argument:
# Define a global variable x with a value of 10
x = 10
# Define a function called my_function that takes in a parameter num
def my_function(num):
# Print the value of the parameter num
print(num)
# Define a nested function called inner_func
def inner_func():
# Use the global keyword to modify the global variable x
global x
# Add 5 to the global variable x
x += 5
# Print the updated value of x
print(x)
# Call the nested function inner_func
inner_func()
# Print the value of the parameter num again
print(num)
# Call the function my_function and pass in the global variable x as an argument
my_function(x)
# Output:
# 10
# 15
# 10
# Explanation:
# The global keyword is used to modify a variable that was defined outside of any functions.
# In this case, the global variable x is modified within the nested function inner_func.
# However, this is not recommended as it can lead to unexpected behavior and make the code harder to understand.
In this example, we’re passing the global variable `x` as an argument to our function. This way, it’s clear that we’re modifying a specific value and not accidentally changing something else in our code.
The difference between globals and locals in Python. Remember: use them wisely and sparingly. And if you ever find yourself using the `global` keyword, ask yourself if there’s a better way to do things.