Python’s globals()
function returns a dictionary of name --> object
mappings. The names are the ones defined globally, i.e., defined by Python or in the outside scope of your program. The objects are the values associated to these names. For example, if you set variable x = [1, 2, 3]
, the globals()
dictionary will contain a name 'x'
and an object reference to [1, 2, 3]
.
Related Tutorial: Namespaces Made Simple
Usage Examples
Learn by example! Here’s a formatted example of how to use the globals()
built-in function:
>>> x = [1, 2, 3] >>> globals() {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'C:\\Users\\...\\code.py', 'x': [1, 2, 3]}
You define a new variable named 'x'
and set it to the list object [1, 2, 3]
. This variable is defined on the global module level, not within a local function scope etc. If you call the globals()
function, it gives you the dictionary of name --> object
mappings. Among the mappings in the dictionary, you find the mapping 'x': [1, 2, 3]
.
You can also modify a global variable using the globals()
dictionary:
>>> friend = 'Alice' >>> globals()['friend'] = 'Bob' >>> friend 'Bob'
This is useful if you want to modify some dynamic variables that you don’t yet know at programming time. For example, you want the user to give you a string of a variable to update. Your program only has the string, so the only way to update the associated global name is to use the globals()
dictionary.
Video globals()
So, how does the syntax formally look like?
Syntax globals()
You use the globals()
method without an argument.
Syntax:
globals() # Returns dictionary of name --> object pairs defined in your global scope.
Arguments | - | – |
Return Value | dict | Returns the dictionary of (name –> object) mappings of all names you can use in your program. This includes the name you defined in the global scope (not within local scopes such as functions) and names defined by Python such as __name__ . |
Python globals() Return Value
Returns the dictionary of (name –> object) mappings of all names you can use in your program. This includes the name you defined in the global scope (not within local scopes such as functions) and names defined by Python such as __name__
.
g = globals() print(type(g)) # <class 'dict'>
Python globals() Interactive Shell Exercise
Let’s have a practical example how to use the globals() function in a real application that asks the user to type in any variable to check its current value:
Exercise: Run the code and find out as a user, the current selection of the variable age
.
Check out my new Python book Python One-Liners (Amazon Link).
If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!
The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).
Publisher Link: https://nostarch.com/pythononeliners
Solution: The interactive exercise can be solved easily by typing in the string 'age'
. Python will then retrieve the value associated to the name from the globals()
dictionary and give you the defined age.
Summary
Python’s globals()
function returns a dictionary of name --> object
mappings.
- The names are the ones defined globally, i.e., defined by Python or in the outside scope of your program.
- The objects are the values associated to these names.
For example, if you set variable x = [1, 2, 3]
, the globals()
dictionary will contain a name 'x'
and an object reference to [1, 2, 3]
.
>>> x = [1, 2, 3] >>> globals() {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'C:\\Users\\...\\code.py', 'x': [1, 2, 3]}
I hope you enjoyed the article! To improve your Python education, you may want to join the popular free Finxter Email Academy:
Do you want to boost your Python skills in a fun and easy-to-consume way? Consider the following resources and become a master coder!
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.