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 will discuss scope. Can anyone tell me what scope refers to in programming?
Isn't it about where you can use a variable in your code?
Exactly! Scope defines where a variable is accessible. It can be local, global, enclosing, or built-in. Who can give me an example of a local variable?
A variable that is defined inside a function, right?
But what about a global variable?
Great question! A global variable is defined outside any function and can be accessed anywhere in the code. Let's remember the acronym LEGB: Local, Enclosing, Global, Built-in. It helps us recall the order in which Python checks the scope!
So if I have both a global and local variable with the same name, which one does Python use?
Python will prioritize the local variable over the global one in that scope. Now, can anyone summarize what we've learned about scope?
Scope is about where variables can be used, and there are different types!
Exactly! Well done, everyone.
Now, let’s dive deeper into the LEGB rule. Can someone explain what it stands for?
It stands for Local, Enclosing, Global, Built-in.
Correct! This order is crucial for how Python resolves variable names. If we create a variable named x inside a function, can we access the global x from within that function?
Not unless we use the global keyword, right?
That's right! The global keyword tells Python that the variable we want to use is defined globally. Let’s recap—local variables are checked first, then variables in enclosing functions, followed by global variables, and finally built-in names. Can someone provide an example of when this might matter?
If I have a local variable x and I want to change the global x, I'll need to use global x.
Exactly! Remembering the LEGB hierarchy helps in understanding scope resolution.
Let’s now look at an example with global and local variables. If I define x = 10 globally and then create a function that defines x = 5, what do you think happens if I try to print x inside the function?
It will print 5 because the local x takes precedence.
Exactly, now if I want to change the global x to 20 inside the function, what keyword would I use?
You would use the global keyword, right?
Correct! And if we had a nested function that needed to access a variable from a non-global outer function, which keyword would you use?
Nonlocal?
Yes! The nonlocal keyword allows access to a variable in the nearest enclosing scope. Let’s summarize: global variables can be accessed anywhere, local variables can only be accessed in their function, and global and nonlocal keywords help manage scope.
Defines where in a program a variable is accessible.
Information related only to the function in which it is defined.
Variables can be accessed from anywhere in the program.
The order of scope resolution in Python.
Tools for modifying variables in different scopes.
In local bounds, the data stays, in global reach, it freely plays.
Once there was a global variable named x who wanted to join the local functions. But, without the magic word global, it could never truly belong!
Remember LEGB - Local begins, Enclosing around, Global to see, Built-in's always found!
LEGB - L for Local, E for Enclosing, G for Global, and B for Built-in
Term
What does LEGB stand for?
Click to flip
Term
What scope do local variables belong to?
Click to flip
Term
How to modify a global variable inside a function?
Click to flip
Term
What does the nonlocal keyword do?
Click to flip
The region of a program where a variable or function is accessible.
The scope of variables defined within a function.
The scope of variables defined outside any function; accessible globally.
The scope of variables in an outer function when using nested functions.
The scope of names that Python has predefined, such as built-in functions.
The ordering of scope resolution: Local, Enclosing, Global, Built-in.
A variable defined outside all functions that can be accessed globally.
A variable defined within a function, not accessible outside that function.
A keyword used to work with variables in the nearest enclosing scope, not global.
A keyword that allows a function to modify a global variable.
This section discusses the concept of scope in programming, focusing on where variables can be accessed within a program based on their lifetime and defined regions.
The section elaborates on the different types of variable scope—local, global, enclosing, and built-in—along with the rules governing their accessibility. It also highlights the usage of keywords like 'global' and 'nonlocal' to manipulate variable scope effectively.
This section delves into the concept of scope, which dictates where in the program a variable can be accessed. Understanding scope is crucial for avoiding errors and writing efficient code.
The different types of variable scope include:
print and len.To resolve variable names, Python applies the LEGB rule—Local, Enclosing, Global, and Built-in, determining the order of scope resolution.
A global variable retains its value across the program, while a local variable exists only within its function. This section uses examples to illustrate how global variables can be accessed and modified, with explanations of the 'global' and 'nonlocal' keywords which help in altering their scope effectively.
By mastering scope and variable lifetimes, programmers can craft clearer, more efficient, and bug-free code.