Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Today, class, we're discussing global variables. Can anyone tell me what a global variable is?
Isn't a global variable a variable defined outside of functions that can be used anywhere in the code?
Exactly! Global variables are accessible throughout your code, but if we want to change them inside a function, we need to use the global keyword. Who can explain what happens without it?
If we don't use global, the function will think we're trying to create a new local variable.
Correct! And remember, the acronym 'GLOBE' can help us remember that Global variables can be accessed Locally only with the global keyword and that they are dynamic objects.
So, how do we use the global keyword in our functions?
Let’s look at a simple example to clarify this.
Here’s a quick example. Let's say we have a variable x set to 10. If we want to change it inside a function, we would write: global x; x = 20. Can anyone write a small program using this concept?
"Sure! I’ll write:
Now let’s talk about the potential pitfalls of using global variables. Why do you think they might be risky?
They can make the code harder to debug, right?
Yes! They can introduce side effects that are hard to track. Can someone give an example?
If multiple functions modify the same global variable, it can lead to unexpected outcomes depending on the order they’re called.
Exactly! As a rule of thumb, limit global variable usage to situations where absolutely necessary. Always ask if there's a way to pass variables as parameters instead. Let’s reinforce our learning with a quick recap.
Variables declared outside of all functions are global and can be accessed anywhere within the script.
Using global Keyword: When you declare a variable as global inside a function using the global keyword, you tell Python that you want to reference the globally scoped variable instead of creating a new local variable. This allows you to perform operations on the global variable directly.
x = 10
def modify():
global x
x = 20
modify()
print(x) # Output: 20In this example, the function modify() uses the global keyword to modify the global variable x, changing its value from 10 to 20. Without declaring x as global, the function would attempt to create a local variable x instead, leaving the global variable unchanged.
Thus, mastering the global keyword is essential for understanding variable scope and lifetime in Python functions and for effectively modifying global state when necessary.
If you want to change a global state, use global before it's too late!
Imagine a pirate named Captain Global, who carried a treasure map (the variable). Everyone in the crew (functions) wants to find the treasure, but only if they mention Captain Global can they change the map!
Remember: G for Global, L for Local; use the Global keyword to keep things pivotal!
GLOBE: Global variables Are Required to Outsmart Local Variable Errors.
Term
What is the purpose of the global keyword?
Click to flip
Term
Where can global variables be accessed?
Click to flip
Term
What happens to a global variable if modified without the global keyword?
Click to flip
A variable that is declared outside all functions and can be accessed and modified anywhere in the code.
A variable that is declared within a function and can only be accessed within that function.
A keyword in Python that allows a function to modify a global variable.
In this section, we explore the global keyword in Python, which allows modification of global variables inside functions.
The global keyword enables functions to modify variables that are defined outside their scope, allowing the functions to access and update global variables. Understanding this concept is crucial for managing variable scope effectively.
In Python, scopes determine the accessibility of variables throughout the program. Aside from local variables that are accessible only within their function, Python also allows the use of global variables, which can be accessed from anywhere, including inside functions. When we want to modify a global variable within a function, we need to use the global keyword.
global Keyword: When you declare a variable as global inside a function using the global keyword, you tell Python that you want to reference the globally scoped variable instead of creating a new local variable. This allows you to perform operations on the global variable directly.x = 10
def modify():
global x
x = 20
modify()
print(x) # Output: 20In this example, the function modify() uses the global keyword to modify the global variable x, changing its value from 10 to 20. Without declaring x as global, the function would attempt to create a local variable x instead, leaving the global variable unchanged.
Thus, mastering the global keyword is essential for understanding variable scope and lifetime in Python functions and for effectively modifying global state when necessary.