{"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> 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 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 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 Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Fed up with searching various pages for the list of Python Built in Functions<\/a>? Look at the tutorial linked here and explore all coding samples of built-in functions of python.<\/p>\n 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 …<\/p>\n
\nlocals() Function in Python:<\/strong><\/p>\n\n
locals()<\/pre>\n
locals() Function with Examples in Python<\/h2>\n
\n
Using Built-in Functions (Static Input)<\/h3>\n
1)How does Python\u2019s globals() method work?<\/h5>\n
\n
# Take a variable and initialize it with the locals() function.\r\nm = locals()\r\n# Print the above result.\r\nprint(m)\r\n<\/pre>\n
{'__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': {...}}<\/pre>\n
Example-2: How does locals() function within a local scope?<\/h5>\n
\n
# Create a function localNotExist() which returns the value returned\u00a0 by locals() function.\r\n\r\n\r\ndef localNotExist():\r\n return locals()\r\n# Create another function localsExist().\r\n\r\n\r\ndef localsExist():\r\n # Inside the localsExist take a variable and set its value to true.\r\n present = True\r\n # Return the value returned by locals() function.\r\n\r\n return locals()\r\n\r\n\r\n# Inside the main function call localNotExist() function and print it.\r\n# call localsExist() function and print it.\r\nprint('localNotExist :', localNotExist())\r\nprint('localsExist :', localsExist())\r\n<\/pre>\n
localNotExist : {}\r\nlocalsExist : {'present': True}<\/pre>\n
\n