Remember
locals() provides the local namespace at the point of the call. Do not use it as a portable way to rebind function-local names.
locals() provides the local namespace at the point of the call. Do not use it as a portable way to rebind function-local names.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
def show(number):
namespace = locals()
print(namespace["number"])
show(3)Top-level scope
function showlocals() provides the local namespace at the point of the call. Do not use it as a portable way to rebind function-local names.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
def show(number):
namespace = locals()
print(namespace["number"])
show(3)
|
v
DISPLAYED RESULT
3
FINAL BINDINGS
show --> object 1 (function)
object 1: function showChoose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
def show(number):
namespace = locals()
print(namespace["number"])
show(3)Top-level scope
function showThe example has finished. Compare the final output with the values and references above.
3
What do you expect this program to print?
def show(number):
namespace = locals()
print(namespace["number"])
show(3)locals() FunctionThe locals() function in Python returns a dictionary representing the current local symbol table. This means it gives you a snapshot of all local variables and their values at a particular point in your code.
locals()
dict object containing all local variables in the current scope.def show_locals():
a = 10
b = "Hello"
print(locals())
show_locals()
{'a': 10, 'b': 'Hello'}
x = 5
y = 7
print(locals())
{'x': 5, 'y': 7, ...}
In a script, locals() at the global level behaves like globals().
eval() or exec() to evaluate expressions in the current local context.While you can modify the dictionary returned by locals(), these changes may not affect actual local variables (especially inside functions).
def test():
x = 1
loc = locals()
loc['x'] = 99
print("x =", x)
test()
x = 1
Why? Because changes to the dictionary don’t always reflect back in the local scope.
locals() will update variables – it might not.locals() for simple scripts unnecessarily can confuse beginners.Interviewers might ask about the difference between locals() and globals(). Remember: locals() gives variables in the current function or scope, while globals() gives variables at the module level.
locals() returns a dictionary of current local variables.Write a function where you declare 3 variables, print them, and also print locals(). Observe the difference.
def demo():
a = 1
b = 2
c = a + b
print("Sum is:", c)
print("Local variables:", locals())
demo()
Connect what you noticed in the example with the idea below.
locals() provides the local namespace at the point of the call. Do not use it as a portable way to rebind function-local names.
locals() contains only names from imported modules.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.