AppDividend
Latest Code Tutorials

Python globals() Example | globals() Function Tutorial

0

Python globals() Example | globals() Function Tutorial is today’s topic. The globals() function in Python returns the dictionary of the current global symbol table. A symbol table is the data structure which contains all the necessary information about the program. These include variable names, methods, classes, etc.

Python globals()

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

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

  1. Local Symbol Table: It stores information related to the local scope of the program. We can get these details using the locals() function. The local scope could be within the function, within the class, etc.
  2. Global Symbol Table: It stores information related to the global scope of the program. We can get these details using the globals() function. The global scope contains all the functions, variables which are not associated with any class or function.

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

# app.py

print(globals())

See the following output.

➜  pyt python3 app.py
{'__name__': '__main__', '__doc__': None, '__package__': None, 
'__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x10340f2b0>, 
'__spec__': None, '__annotations__': {}, 
'__builtins__': <module 'builtins' (built-in)>, 
'__file__': 'app.py', '__cached__': None}
➜  pyt

One thing to analyze that it also prints a python script from where this function is called. This is what I meant by the current module.

Okay, now let’s define one variable called name and see the output.

# app.py

name = "Millie Bobby Brown"
print(globals())

See the following output.

➜  pyt python3 app.py
{'__name__': '__main__', '__doc__': None, '__package__': None, 
'__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x10340f2e8>, 
'__spec__': None, '__annotations__': {}, 
'__builtins__': <module 'builtins' (built-in)>, 
'__file__': 'app.py', '__cached__': None, 
'name': 'Millie Bobby Brown'}
➜  pyt

You can see that in the output’s last line, the defined variable has appeared.

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

Let’s access the name variable.

# app.py

name = "Millie Bobby Brown"
print(globals()['name'])

See the following output.

➜  pyt python3 app.py
Millie Bobby Brown
➜  pyt

#Modify global variable using global()

See the following code.

# app.py

name = "Millie Bobby Brown"
globals()['name'] = "Joe Keerey"
print(globals()['name'])
print(globals())

Here, since a global symbol table also stores all the global variables, i.e. in this case, name, the value of name can be changed using the globals() function.

The dictionary returned is accessed using the key of the variable name and modified to Joe Keery.

This is reflected in the global symbol table again.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.