Open In App

locals() function-Python

Last Updated : 03 Mar, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The locals() function in Python returns a dictionary of the current local symbol table. It allows access to the local scope’s variables, making it useful for debugging and dynamic operations.

Symbol Table in Python:

A symbol table is a data structure maintained by the Python compiler to store information required for program execution.

  • Local Symbol Table: Stores information about the local scope of a function or block.
  • Global Symbol Table: Maintains information about global variables and functions.

Syntax of locals()

locals()

Parameters: This function does not take any input parameter. 

Return Type: This returns the information stored in local symbol table.

Understanding locals() in Local Scope

locals() function behaves differently based on the presence of local variables.

Example 1: locals() with no local variables

def fun():
    print("No local variable is present:", locals())

fun()

Output
No local variable is present: {}

Explanation: Since there are no local variables inside fun(), locals() returns an empty dictionary.

Example 2: locals with locals variables

def fun():
    name = "Ankit"
    print("Local variables present:", locals())

fun()

Output
Local variables present: {'name': 'Ankit'}

Explanation(): Here, locals() returns a dictionary containing the variable name and its value.

Can locals() modify local variables?

Unlike globals(), which can modify global variables, locals() does not allow modifying local variables inside a function.

Example: Attempting to modify local variables using locals()

def fun():
    name = "Ankit"
    
    # attempting to modify name using locals()
    locals()['name'] = "Sri Ram"
    
    print(name)

fun()

Output
Ankit

Explanation: Even though locals()[‘name’] is modified, the change does not reflect in the actual variable because locals() returns a copy of the local symbol table, not a live reference.

locals in Global scope()

In the global scope, locals() behaves similarly to globals(), as the local and global symbol tables are the same.

Example: locals() in global environment

print(locals())
print(globals())

Output

{'__name__': '__main__', '__doc__': 'Automatically created module for IPython interactive environment', '__package__': None, '__loader__': None, '__spec__': None, '__builtin__': <module 'builtins' (built-in)>, '__builtins__': <module 'builtins' (built-in)>, '_ih': ['', 'print(locals())\nprint(globals())'], '_oh': {}, '_dh': ['/content'], 'In': ['', 'print(locals())\nprint(globals())'], 'Out': {}, 'get_ipython': <bound method InteractiveShell.get_ipython of <google.colab._shell.Shell object at 0x79a00b8f3910>>, 'exit': <IPython.core.autocall.ZMQExitAutocall object at 0x79a00b90aa50>, 'quit': <IPython.core.autocall.ZMQExitAutocall object at 0x79a00b90aa50>, '_': '', '__': '', '___': '', '_i': '', '_ii': '', '_iii': '', '_i1': 'print(locals())\nprint(globals())'}
{'__name__': '__main__', '__doc__': 'Automatically created module for IPython interactive environment', '__package__': None, '__loader__': None, '__spec__': None, '__builtin__': <module 'builtins' (built-in)>, '__builtins__': <module 'builtins' (built-in)>, '_ih': ['', 'print(locals())\nprint(globals())'], '_oh': {}, '_dh': ['/content'], 'In': ['', 'print(locals())\nprint(globals())'], 'Out': {}, 'get_ipython': <bound method InteractiveShell.get_ipython of <google.colab._shell.Shell object at 0x79a00b8f3910>>, 'exit': <IPython.core.autocall.ZMQExitAutocall object at 0x79a00b90aa50>, 'quit': <IPython.core.autocall.ZMQExitAutocall object at 0x79a00b90aa50>, '_': '', '__': '', '___': '', '_i': '', '_ii': '', '_iii': '', '_i1': 'print(locals())\nprint(globals())'}

Explanation: Since both locals() and globals() refer to the same dictionary in the global scope, their outputs are identical.

global() vs locals()-Comparison Table

Feature

globals()

locals()

Returns

Global symbol table

Local symbol table

Scope

Global variable

Local variables inside a function

Modifiable?

Yes, modifies global variables

No, does not modify local variables

Access global variables?

Yes

No (inside a function)

Behavior in global scope

Same as locals()

Same as globals()

Used for?

Modifying and accessing global variables

Inspecting local variables

Python | locals() function – FAQs

What are locals in Python?

The locals() function in Python retrieves a dictionary containing all the information stored in the local symbol table. The local symbol table is generated by the compiler and holds the necessary information for the execution of a program.

What are local variables in Python?

Local variables in Python are those specifically defined within a block of code, such as within a function. These variables have a limited scope and are accessible only within the confines of that block; attempting to use them outside of it will result in an error. To illustrate, a variable declared within a function is termed a local variable, and its scope is restricted to that particular function.

What are globals in Python?

Global variables in Python are accessible throughout the entire program, having been defined outside any specific function block. Their scope is considered global, allowing them to be utilized across different parts of the code. The globals() function facilitates access to information stored within the global symbol table. This table acts as a data structure containing essential details pertaining to the program’s global scope, playing a vital role in the program’s execution.

Does Python have local variables?

Yes, Python does have local variables. Local variables are variables that are defined within a specific block of code, such as inside a function or a loop. These variables have a limited scope and are only accessible within the block in which they are defined. Once the block is exited, the local variables are no longer available.



Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg