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 are going to discuss the LEGB rule in Python, which is crucial for understanding how variable names are resolved. Can anyone guess what each letter stands for?
I think 'L' could stand for Local variables?
Correct, 'L' is for Local! Local variables are defined within the current function. Now, what about 'E'?
'E' could be Enclosing, right? Like in nested functions?
Exactly! Enclosing scope refers to variables in surrounding functions. Let's move on to 'G'!
Is 'G' for Global variables that are defined outside of functions?
Right again! Global variables can be accessed from anywhere in the module, unless shadowed by local variables. Finally, what about 'B'?
'B' stands for Built-in variables, like print or len?
Perfect! Built-in scopes include names that Python has predefined.
To summarize, the LEGB rule prioritizes Local first, then Enclosing, then Global, and last, Built-in.
Let’s look at an example. If I define x = 20 globally and then within a function, I redefine x = 10. What happens when I print x within that function?
We would see 10 printed, since it’s local now.
Exactly! The local variable overshadows the global one. What if we want to access the global variable x inside the function?
We could use the global keyword to specify that we mean the global variable.
Correct! Using global x allows us to modify the global variable inside the function. Let’s try implementing a small code snippet together!
Let’s talk about some pitfalls. What do you think happens if we declare a variable 'x' inside a nested function without the 'nonlocal' keyword?
We might get an error or it’d create a new local variable instead?
Correct! Without 'nonlocal,' the inner function creates its own 'x' rather than accessing the enclosing 'x'. When is 'nonlocal' useful?
When you want to modify a variable from an outer, non-global scope?
Exactly! Just remember to use 'global' for global variables and 'nonlocal' for enclosing scope. It helps avoid unintended behavior!
Why is understanding LEGB key to writing clean code?
So we don’t overwrite important variables by mistake?
Absolutely! It ensures our code is safe from potential bugs due to name clashes.
And using consistent naming helps too, right?
Yes, good variable naming conventions make a significant difference. Let's always try to make variable scopes clear to improve the readability of our code.
In summary, remember the LEGB order—this will help you clarify which variable you're accessing and make your code cleaner!
The order in which Python looks for variable names: Local, Enclosing, Global, Built-in.
These are defined within a function and cannot be accessed outside.
Variables defined outside of function scopes and can be accessed globally.
These are accessible in nested functions from their enclosing function.
Predefined names in Python that are always available.
Example of Local Variable:
def my_function():
x = 10 # Local variable
print(x)
my_function() # Outputs: 10Example of Global Variable:
x = 20 # Global variable
def my_function():
print(x)
my_function() # Outputs: 20Example showing Enclosing Scope:
def outer_function():
x = 30
def inner_function():
print(x) # Accessing x from outer_function
inner_function()
outer_function() # Outputs: 30Example using nonlocal:
def outer_function():
x = 10
def inner_function():
nonlocal x
x += 5
print(x)
inner_function() # Outputs: 15
outer_function()When programming with Python, don’t be daunted, Remember LEGB, and you’ll be undaunted! Local first, Enclosing next, Global’s in sight, And Built-in names, always shining bright!
Once in a coding kingdom, lived a Local Variable named Larry, who could only play in his function. Enclosing Eddie watched him from his outer function, while Global Greg helped all over the module. But in times of need, Built-in Betty always saved the day with her magic names! So, when trouble arose, they used LEGB to solve their variable plight!
L: Local, E: Enclosing, G: Global, B: Built-in. Keep LEGB in line, and you'll never lose track of your variables!
LEGB: Think of it as a line-up for variables – Local comes first, Enclosing comes next, then Global, and don’t forget the Built-ins!
Term
What does the acronym LEGB represent?
Click to flip
Term
What keyword is used to declare a global variable inside a function?
Click to flip
Term
What does 'nonlocal' keyword do?
Click to flip
Term
What is the scope of a variable defined inside a function?
Click to flip
Variables defined within a function, only accessible inside that function.
Variables in the local scope of enclosing functions when nested.
Variables defined at the top level of a script or module, accessible throughout.
Names preassigned in Python’s built-in namespace, like print or len.
A declaration inside a function to modify a global variable.
A declaration to modify a variable in an enclosing function scope.
The LEGB rule describes the order in which Python resolves variable names, identifying scopes as Local, Enclosing, Global, and Built-in.
Understanding the LEGB rule is essential for Python programmers, as it dictates how variable names are resolved based on their scope. Variables can be local to a function, global to the module, in enclosing functions, or predefined by Python itself, impacting how code behaves and preventing name clashes.
The LEGB rule is a foundational concept in Python programming that illustrates how variable names are resolved. This acronym stands for:
len and print.Understanding the LEGB rule is crucial for effective coding practices in Python to avoid variable shadowing—where a local variable overshadows a global variable with the same name. This section will explore each of these scopes, illustrated with examples and best practices, aiding in writing clean, efficient, and error-free code.