Remember
globals() returns the current global namespace dictionary. Its keys are names, and its values are their bound objects.
globals() returns the current global namespace dictionary. Its keys are names, and its values are their bound objects.
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.
number = 3
namespace = globals()
print(namespace["number"])Top-level scope
3Object 2 · dict{'__name__': '__main__', 'number': 3, 'namespace': {'__name__': '__main__', 'number': 3, 'namespace': {…: …, …: …, …: …}}}globals() returns the current global namespace dictionary. Its keys are names, and its values are their bound objects.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
number = 3
namespace = globals()
print(namespace["number"])
|
v
DISPLAYED RESULT
3
FINAL BINDINGS
number --> object 1 (int)
namespace --> object 2 (dict)
object 1: 3
object 2: {'__name__': '__main__', 'number': 3, 'namespace': {'__name__'...Choose 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.
number = 3
namespace = globals()
print(namespace["number"])Top-level scope
3Object 2 · dict{'__name__': '__main__', 'number': 3, 'namespace': {'__name__': '__main__', 'number': 3, 'namespace': {…: …, …: …, …: …}}}The example has finished. Compare the final output with the values and references above.
3
What do you expect this program to print?
number = 3
namespace = globals()
print(namespace["number"])globals() FunctionThe globals() function in Python returns a dictionary representing the current global symbol table. This includes all global variables, functions, and imported modules available at the time of the call.
globals()
globals() does not take any arguments.x = 10
def greet():
return "Hello"
global_vars = globals()
print(global_vars["x"])
print(global_vars["greet"]())
10
Hello
globals()["new_var"] = "Python is fun!"
print(new_var)
Python is fun!
Note: We added a global variable new_var without declaring it directly in the code.
globals() inside a function and expecting it to return local variables — it won’t.If you're asked about dynamic variable creation or variable scope, mention globals() as a tool for accessing and manipulating the global namespace.
globals() returns the dictionary of current global variables.Write a program that adds a new global variable using globals() and prints it:
globals()["language"] = "Python"
print(language)
Connect what you noticed in the example with the idea below.
globals() returns the current global namespace dictionary. Its keys are names, and its values are their bound objects.
globals() returns a list of only local function parameters.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.