{"id":24837,"date":"2021-11-02T09:54:27","date_gmt":"2021-11-02T04:24:27","guid":{"rendered":"https:\/\/python-programs.com\/?p=24837"},"modified":"2021-11-05T20:37:34","modified_gmt":"2021-11-05T15:07:34","slug":"python-locals-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-locals-function-with-examples\/","title":{"rendered":"Python locals() Function with Examples"},"content":{"rendered":"

In the previous article, we have discussed Python hasattr() Method with Examples<\/a>
\nlocals() Function in Python:<\/strong><\/p>\n

The locals() method updates the current local symbol table and returns a dictionary of its contents.<\/p>\n

A symbol table is a data structure maintained by a compiler that contains all necessary program information.<\/p>\n

Variable names, methods, classes, and so on are examples of this.<\/p>\n

There are two types of symbol tables.<\/p>\n

    \n
  1. Global symbol table<\/li>\n
  2. Local symbol table<\/li>\n<\/ol>\n

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

    All functions and variables that are not associated with any class or function are included in the global scope.<\/p>\n

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

    The local scope could be within a function, a class, or something else.<\/p>\n

    Syntax:<\/strong><\/p>\n

    locals()<\/pre>\n

    Parameter Values:\u00a0<\/strong>The locals() method does not accept any parameters.<\/p>\n

    Return Value:<\/strong><\/p>\n

    The dictionary associated with the current local symbol table is updated and returned by the locals() method.<\/p>\n

    locals() Function with Examples in Python<\/h2>\n