Skip to main content

Python globals() Function

The globals() method returns the global symbol table as a dictionary. It contains information about all global variables.

Syntax

globals()

globals() Parameters

Python globals() function has no parameters.

globals() Return Value

Python globals() function returns the dictionary of the current global symbol table.

Each key in the dictionary holds the name of the variable. This dictionary is also accessible from within functions and can be used to update global variables directly.

Example

# Declare some global variables
a = 10
b = 20
c = 30
d = 40

# Print the current global symbol table
print(globals())

# Prints {'__name__': '__main__',
# '__doc__': None,
# '__package__': None,
# '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x7fed1a254358>,
# '__spec__': None,
# '__annotations__': {},
# '__builtins__': <module 'builtins' (built-in)>,
# '__file__': 'main.py',
# '__cached__': None,
# 'a': 10,
# 'b': 20,
# 'c': 30,
# 'd': 40 }

Symbol Table

What is a symbol table?

A symbol table is maintained by a compiler. It contains necessary information about the current program.

There are two kinds of symbol table:

  • Local symbol table contains information related to the local scope of the program
  • Global symbol table contains information related to the global scope of the program

Why is it necessary?

Python gives the programmer a large number of tools for introspecting the running environment. globals() is just one of those, and it can be very useful in a debugging session to see what objects the global scope actually contains.

Global and Local Scope

A variable declared outside a function has a Global Scope and can be accessed inside or outside of a function. But you can not update it inside a function.

However, a variable declared within a function has a Local Scope and can only be accessed within that function.

x = 5       # global scope

def myfunc():
x = 10 # local scope
print('x inside function is', x)

myfunc() # Output: x inside function is 10

print('x outside function is', x) # Output: x outside function is 5

Here, the value of global variable x did not change. Because, Python created a new local variable named x; which disappears when the function ends, and has no effect on the global variable.

Access global variables

As globals() method returns a dictionary, you can perform any operation that a dictionary supports, like indexing, iteration etc.

For example, get the value of global variable x:

x = 10
print(globals()['x']) # Output: 10

output

10

You can access other global variables such as filename, package, or docstring from current global symbol table.

For example, get the filename of the current program:

print(globals()['__file__'])    # Output: main.py

output

main.py

Modify global variables

Using globals() function you can update a global variable from a no-global scope, e.g. inside a function.

For example, update global variable x inside a function:

x = 5       # global scope

def myfunc():
x = 10 # local scope
globals()['x'] = 123 # update global x
print('x inside function is', x)

myfunc() # Output: x inside function is 123
print('x outside function is', x) # Output: x outside function is 123

output

x inside function is 10
x outside function is 123