Python locals() Function with Examples

In the previous article, we have discussed Python hasattr() Method with Examples
locals() Function in Python:

The locals() method updates the current local symbol table and returns a dictionary of its contents.

A symbol table is a data structure maintained by a compiler that contains all necessary program information.

Variable names, methods, classes, and so on are examples of this.

There are two types of symbol tables.

  1. Global symbol table
  2. Local symbol table

A global symbol table stores all information related to the program’s global scope and is accessed in Python via the globals() method.

All functions and variables that are not associated with any class or function are included in the global scope.

Similarly, the Local symbol table stores all information related to the program’s local scope and is accessed in Python via the locals() method.

The local scope could be within a function, a class, or something else.

Syntax:

locals()

Parameter Values: The locals() method does not accept any parameters.

Return Value:

The dictionary associated with the current local symbol table is updated and returned by the locals() method.

locals() Function with Examples in Python

Using Built-in Functions (Static Input)

1)How does Python’s globals() method work?

Approach:

  • Take a variable and initialize it with the locals() function.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Take a variable and initialize it with the locals() function.
m = locals()
# Print the above result.
print(m)

Output:

{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x7f65e0415cc0>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': '/home/29fd5ae710f8ed30944276be6a192e84.py', '__cached__': None, 'm': {...}}
Example-2: How does locals() function within a local scope?

Approach:

  • Create a function localNotExist() which returns the value returned  by locals() function.
  • Create another function localsExist().
  • Inside the localsExist take a variable and set its value to true.
  • Return the value returned by locals() function.
  • Inside the main function call localNotExist() function and print it.
  • call localsExist() function and print it.
  • The Exit of the Program.

Below is the implementation:

# Create a function localNotExist() which returns the value returned  by locals() function.


def localNotExist():
    return locals()
# Create another function localsExist().


def localsExist():
  # Inside the localsExist take a variable and set its value to true.
    present = True
    # Return the value returned by locals() function.

    return locals()


# Inside the main function call localNotExist() function and print it.
# call localsExist() function and print it.
print('localNotExist :', localNotExist())
print('localsExist :', localsExist())

Output:

localNotExist : {}
localsExist : {'present': True}

Fed up with searching various pages for the list of Python Built in Functions? Look at the tutorial linked here and explore all coding samples of built-in functions of python.