Python Reference Library →

Python globals() Function – Access Global Symbol Table

Let’s look at globals() function – access global symbol table

  1. Notice

    globals() returns the current global namespace dictionary. Its keys are names, and its values are their bound objects.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
number = 3
namespace = globals()
print(namespace["number"])
Live state

Top-level scope

number→Object 1
namespace→Object 2
Object 1 · int3Object 2 · dict{'__name__': '__main__', 'number': 3, 'namespace': {'__name__': '__main__', 'number': 3, 'namespace': {…: …, …: …, …: …}}}
This step

globals() returns the current global namespace dictionary. Its keys are names, and its values are their bound objects.

Picture globals() function – access global symbol table

globals() returns the current global namespace dictionary. Its keys are names, and its values are their bound objects.

Image brief

Alt text
A diagram for Python globals() Function – Access Global Symbol Table. Instructions: number = 3 namespace = globals() print(namespace["number"]). Displayed result: 3. The final example bindings are number --> object 1 (int); namespace --> object 2 (dict). object 1: 3; object 2: {'__name__': '__main__', 'number': 3, 'namespace': {'__name__'...
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python globals() Function – Access Global Symbol Table". Teaching focus: globals() returns the current global namespace dictionary. Its keys are names, and its values are their bound objects. Depict exactly the relationships and labels in this reference diagram: 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__'... Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python globals() Function – Access Global Symbol Table. Instructions: number = 3 namespace = globals() print(namespace["number"]). Displayed result: 3. The final example bindings are number --> object 1 (int); namespace --> object 2 (dict). object 1: 3; object 2: {'__name__': '__main__', 'number': 3, 'namespace': {'__name__'...

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
number = 3
namespace = globals()
print(namespace["number"])
Live state

Top-level scope

number→Object 1
namespace→Object 2
Object 1 · int3Object 2 · dict{'__name__': '__main__', 'number': 3, 'namespace': {'__name__': '__main__', 'number': 3, 'namespace': {…: …, …: …, …: …}}}
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
3

What do you expect this program to print?

What does this example display?

number = 3
namespace = globals()
print(namespace["number"])
What does this example display?
Choose one answer. You can try again.

Answer and explanation

3

Follow the printed values in order. The output is: 3

More examples and reference

Python globals() Function

The 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.

Syntax

globals()

Parameters:

  • None – globals() does not take any arguments.

Returns:

  • A dictionary representing the current global symbol table.

Example 1: View Global Variables

x = 10
def greet():
    return "Hello"

global_vars = globals()
print(global_vars["x"])
print(global_vars["greet"]())
10
Hello

Example 2: Add a Global Variable Dynamically

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.

Use Case: When Should You Use globals()?

  • To inspect all global variables and their values at runtime.
  • To dynamically access or set global variables by name.
  • In scripting environments or advanced meta-programming scenarios.

Common Mistakes

  • Using globals() inside a function and expecting it to return local variables — it won’t.
  • Modifying the global symbol table inappropriately can lead to code that is hard to understand and debug.

Interview Tip

If you're asked about dynamic variable creation or variable scope, mention globals() as a tool for accessing and manipulating the global namespace.

Summary

  • globals() returns the dictionary of current global variables.
  • You can read and write global values using it.
  • Use with caution — overusing it can reduce code readability.

Practice Problem

Write a program that adds a new global variable using globals() and prints it:

globals()["language"] = "Python"
print(language)

What to remember

Connect what you noticed in the example with the idea below.

Remember

globals() returns the current global namespace dictionary. Its keys are names, and its values are their bound objects.

Watch out for this idea

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.

    Reference library →