Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is the difference between dir(), globals() and locals() functions in Python?
The Python built-in functions, dir(), globals(), and locals() are used to provide insights into the objects, variables, and identifiers present in various scopes.
They might look similar, but each function serves a different purpose and behaves differently depending on where and how it is used.
Python dir() Function
The Python dir() function is used to list the names in the current local scope or the attributes of an object. If no argument is passed, it returns the list of names in the current local scope.
dir(object)
Example
Let's look at the following example, where we are going to use the dir() function without arguments −
a = 11
def demo():
b = 12
print(dir())
demo()
['b']
Using dir() with Objects
You can also pass an object to dir() to see its attributes and methods −
numbers = [1, 2, 3] print(dir(numbers)[:5]) # Show first 5 attributes
['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__']
Python globals() Function
The Python globals() function returns a dictionary representing the current global symbol table. It includes all the global variables and functions defined at the module level.
globals()
Example
In the following example, we are going to use the globals() function to access the global variables −
a = 112 b = "TutorialsPoint" print(globals()['a']) print(globals()['b'])
112 TutorialsPoint
Python locals() Function
The Python locals() function returns a dictionary representing the current local symbol table. When invoked inside a function, it reflects the function's local namespace.
locals()
Example
Consider the following example, where we are going to use the locals() inside a function to inspect all the local variables −
def demo():
a = "Welcome"
b = "Hi"
print(locals())
demo()
{'a': 'Welcome', 'b': 'Hi'}
Key Differences
| Function | Returns | Scope | Output Type |
|---|---|---|---|
dir() |
List of names/attributes | Current local or object attributes | List |
globals() |
Global symbol table | Global/module level | Dictionary |
locals() |
Local symbol table | Current local scope | Dictionary |
Practical Comparison
Here's a comprehensive example showing all three functions in action −
global_var = "I am global"
def compare_functions():
local_var = "I am local"
print("dir():", dir())
print("\nlocals():", locals())
print("\nglobals() keys (first 5):", list(globals().keys())[:5])
compare_functions()
dir(): ['local_var']
locals(): {'local_var': 'I am local'}
globals() keys (first 5): ['__name__', '__doc__', '__package__', '__loader__', '__spec__']
Conclusion
Use dir() to list available names or object attributes, globals() to access global variables as a dictionary, and locals() to inspect local variables within functions. Each serves a specific purpose in Python introspection and debugging.
