Python globals()

Filed Under: Python

Python globals() function return a dictionary representing the current global symbol table.

Python program maintains program information in symbol tables. There are two types of symbol tables:

  1. Local Symbol Table – stores information related to the local scope of the program. We can get this details using locals() function.
  2. Global Symbol Table – stores information related to global scope of the program. We can get this details using globals() function.

Python symbol table contains details about variable names, methods, classes, etc.

The global scope contains details about functions, variables, modules that are not associated with any class or function.

Python globals()

Python globals() function doesn’t have any arguments. This function returns the dictionary of the current module (inside a function or method, this is the module where it is defined, not the module from which it is called).

Let’s see what is returned by globals() function.


print(globals())

Output:


{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x10a99b358>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': '/Users/pankaj/Documents/github/journaldev/Python-3/basic_examples/python_globals_example.py', '__cached__': None}

Note that it also prints the python script from where this function is called. This is what meant by the current module. Let’s see the output if the same function is called from the Python3.7 console.


$python3.7
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print(globals())
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>}
>>> 

Notice that this time there is no __file__ attribute in the output.

Python globals() example

As we mentioned earlier that the global symbol table contains information about the global variables too. Let’s see this with a simple example.


name = 'Pankaj'

print(globals())

Output:


{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x10a99b358>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': '/Users/pankaj/Documents/github/journaldev/Python-3/basic_examples/python_globals_example.py', '__cached__': None, 'name': 'Pankaj'}

Notice that the global symbol dictionary contains ‘name’ variable too.

One of the features of globals() is that we can modify the variables in the global dictionary. Since it’s a dictionary, we can get the value of a specific key too.


globals()['name'] = 'David'

print(globals()['name'])

Output: David

Let’s see if globals() contains local scope variables or not. For this, let’s define a function and some variables inside it.


def fx():
    local = ''
    global gl
    gl = 'Global'


fx()  # calling fx() to set the variables
print(globals())
print(globals()['gl'])

Output:


{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x10a99b358>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': '/Users/pankaj/Documents/github/journaldev/Python-3/basic_examples/python_globals_example.py', '__cached__': None, 'name': 'David', 'fx': <function fx at 0x10a9331e0>, 'gl': 'Global'}
Global

Note that ‘gl’ variable is part of the global dictionary because its scope is global. Whereas, ‘local’ variable is not part of the global dictionary because of having local scope.

Conclusion

Python globals() function is a utility mostly used in debugging the code. We can use it to identify the global scope variables and functions available to the program. We can also modify this data, but that is not advisable and mostly not required in normal programs.

You can checkout complete python script and more Python examples from our GitHub Repository.

Reference: Official Documentation

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages