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, we'll explore how we can access and modify global variables using the global keyword. Can anyone tell me what a global variable is?
Isn't it a variable that can be accessed from anywhere in the program?
Exactly! A global variable is declared outside any function. Now, using the global keyword allows us to modify this variable inside a function. For example, if I have x = 5 and I want to change it inside a function, I use the global keyword.
Could you give us an example?
Sure! Here’s how it looks. x = 5 and then in the function, we write global x; x = 10. After calling the function, if we print x, it will show 10.
I see! So, it’s about taking control over global variables from within a function.
Exactly! Remember: global variables are modified with the keyword where necessary.
So, if I miss global inside the function, I won’t be able to change the global variable?
Correct! Without it, Python treats x as a local variable. Great question! Let’s summarize: You must declare global variables with the global keyword if you intend to modify them within a function.
Now, let's shift to the nonlocal keyword. This keyword is used in nested functions to modify a variable in the outer scope. Can someone explain what nested functions are?
They are functions defined inside another function, right?
Correct! With nested functions, we can use nonlocal to access variables from the enclosing function. For instance, if y is defined in the outer function, how might we change its value in the inner function?
By declaring it as nonlocal in the inner function?
Yes! Let's see an example together. In the outer function, we define y = 10 and then in the inner function, we write nonlocal y; y = 20. After calling the outer function, it prints the updated value of y.
So, using nonlocal allows us to modify the variable defined in the outer function's scope?
Precisely! This is vital when you want to retain the context of your variables in closures. To wrap up: nonlocal helps in modifying non-global variables in outer functions.
Let's discuss the implications of using global and nonlocal. Why do you think we might want to avoid using global variables unnecessarily?
They can create confusion about the state if many functions modify them?
Exactly! Overuse of global variables can lead to hard-to-trace errors. It's often regarded as better practice to keep them to a minimum!
And what about nonlocal? Are there pitfalls we should watch for?
Great question! Nonlocal can also introduce complications, especially with larger nested structures. If not handled carefully, modifying outer variables can lead to unintended side effects.
So, handling variables well is essential for code readability and maintainability!
Absolutely! Keeping scope considerations in mind helps in writing clean and manageable code. To summarize, be cautious with global & nonlocal to maintain clarity in your code!
A variable that can be accessed from anywhere in the program.
Used to modify a global variable inside a function.
A variable found in an enclosing scope, allowing nested functions to modify it.
Declares a nonlocal variable in a nested function.
Defines the accessibility area of a variable.
Global is wide, reaches far and near, inside functions, it's the keyword we hold dear.
Once in a function village, there was a global river that everyone could access. But in a small nested forest nearby, only the nonlocal paths knew how to adjust the small creek's flow.
G-N for the keywords: G for global for the overarching access, N for nonlocal for deeper-nested scope changes.
Remember LEGB: Local, Enclosing, Global, Built-in, the order Python uses to resolve variable names.
Term
Global Variable
Click to flip
Term
Global Keyword
Click to flip
Term
Nonlocal Variable
Click to flip
Term
Nonlocal Keyword
Click to flip
Term
Scope
Click to flip
A variable that is defined outside any function and can be accessed from anywhere in the code.
A keyword used to declare a global variable inside a function.
A variable that is defined in an enclosing function and can be accessed or modified in nested functions.
A keyword used to declare a nonlocal variable in a nested function.
The region in the code where a variable is accessible.
The global and nonlocal keywords in Python allow function-level access to variables defined outside their immediate scope.
This section discusses the significance of the global and nonlocal keywords in Python. The global keyword modifies global variables within functions, while the nonlocal keyword enables access to variables defined in enclosing (but not global) scopes, typically used in nested functions. Understanding these keywords is vital for managing variable scope effectively.
In Python, scope defines where variables are accessible and can vary based on how and where they are defined. The global keyword is utilized within a function to indicate that a variable is defined at the module level. This allows functions to modify global variables directly. For example:
x = 5
def change():
global x
x = 10
change()
print(x) # Output: 10In the example above, the change function modifies the global variable x, which is initialized with the value 5.
On the other hand, the nonlocal keyword is pertinent in nested functions, allowing them to modify variables defined in the outer function's scope without directly accessing global variables. This is crucial when dealing with closures where you want to preserve or modify the state of outer function variables.
def outer():
x = 5
def inner():
nonlocal x
x = 10
inner()
print(x) # Output: 10
outer()In summary, understanding these keywords is essential for effective variable management within different scopes and to ensure the correct modification of variable values in structured Python programs.