Python locals() function returns a dictionary representing the current local symbol table.
Python program maintains program information in symbol tables. There are two types of symbol tables:
- Local Symbol Table – stores information related to the local scope of the program. We can get this detail using locals() function.
- Global Symbol Table – stores information related to the global scope of the program. We can get this detail using globals() function.
Python symbol table contains details about variable names, methods, classes, etc.
Python locals()
Python locals() function doesn’t take any argument. Let’s see the dictionary returned by locals() function.
print(locals())
Output:
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x10ab79358>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': '/Users/pankaj/Documents/github/journaldev/Python-3/basic_examples/python_locals_example.py', '__cached__': None}
If you will execute print(globals())
, you will get the same output. However, the output might vary a little based on your Python installation.
So where is the difference between locals() and globals()?
There is no difference because we are executing locals() and globals() in the current module itself. The difference will be present when we call these functions inside a method or class.
Python locals() inside method
Let’s see what is the output when locals() and globals() are invoked inside a function body.
# locals() inside function
def fx1():
var = 1
global gl
gl="x"
print('nlocals() value inside functionn', locals())
print('nglobals() value inside functionn', globals())
fx1()
Output:
locals() value inside function
{'var': 1}
globals() value inside function
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x10277c358>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': '/Users/pankaj/Documents/github/journaldev/Python-3/basic_examples/python_locals_example.py', '__cached__': None, 'fx1': <function fx1 at 0x1027141e0>, 'gl': 'x'}
So it’s clear that locals() inside function returns the local variable, notice that global variables are part of global symbol table dictionary.
Python locals() inside class
Let’s see the output when locals() is called inside class body.
# locals() inside class
class Data:
x = 0
print('nlocals() value inside classn', locals())
print('nglobals() value inside classn', globals())
Output:
locals() value inside class
{'__module__': '__main__', '__qualname__': 'Data', 'x': 0}
globals() value inside class
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x10277c358>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': '/Users/pankaj/Documents/github/journaldev/Python-3/basic_examples/python_locals_example.py', '__cached__': None, 'fx1': <function fx1 at 0x1027141e0>, 'gl': 'x'}
When invoked inside the class body, locals() contains the module name, class name and class variables.
Conclusion
Python locals() function is mostly used for debugging purpose. We can check what variables are present in the local scope and their values. We can also alter their values since it’s a dictionary, although not recommended.
Reference: Official Documentation