{"id":24817,"date":"2021-11-02T09:48:08","date_gmt":"2021-11-02T04:18:08","guid":{"rendered":"https:\/\/python-programs.com\/?p=24817"},"modified":"2021-11-05T20:37:13","modified_gmt":"2021-11-05T15:07:13","slug":"python-globals-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-globals-function-with-examples\/","title":{"rendered":"Python globals() Function with Examples"},"content":{"rendered":"
In the previous article, we have discussed Python filter() Function with Examples<\/a> The globals() function returns a dictionary containing the global symbol table.<\/p>\n A symbol table contains information about the current program that is required.<\/p>\n Variable names, methods, classes, and so on are examples of this.<\/p>\n There are two types of symbol tables.<\/p>\n 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 Similarly, 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 Syntax:<\/strong><\/p>\n Parameter Values: <\/strong>The globals() method does not accept any parameters.<\/p>\n Return Value:<\/strong><\/p>\n This method returns the dictionary containing the current global symbol table.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n All global variables and other symbols for the current program are displayed in the output.<\/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 <\/p>\n","protected":false},"excerpt":{"rendered":" In the previous article, we have discussed Python filter() Function with Examples globals() Function in Python: The globals() function returns a dictionary containing the global symbol table. A symbol table contains information about the current program that is required. Variable names, methods, classes, and so on are examples of this. There are two types of …<\/p>\n
\n globals() Function in Python:<\/strong><\/p>\n\n
globals()<\/pre>\n
globals() Function with Examples in Python<\/h2>\n
\n
1)Using global variables, make changes to global variables ()<\/h5>\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the number(id) as static input and store it in a variable.\r\ngvn_id = 10\r\n# Modify the given number to some random number by using the globals() function.\r\nglobals()['gvn_id'] = 5\r\n# Print the new number after modification.\r\nprint('The modified Id is:', gvn_id)\r\n<\/pre>\n
The modified Id is: 5<\/pre>\n
How does Python’s globals() method work?<\/h5>\n
# Take a variable and initialize it with the globals() function.\r\nm = globals()\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 0x7f0e651f0cc0>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': '\/home\/78f96300568d7239be1e5414d328ce86.py', '__cached__': None, 'm': {...}}<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the number(id) as user input using the int(input()) function and store it in a variable.\r\ngvn_id = int(input(\"Enter some random number = \"))\r\n# Give the new number as user input using the int(input()) function and store it in another variable.\r\nnew_id = int(input(\"Enter some random number = \"))\r\n# Modify the given number to the above given new number by using the globals() function.\r\nglobals()['gvn_id'] = new_id\r\n# Print the new number after modification.\r\nprint('The modified Id is:', gvn_id)\r\n<\/pre>\n
Enter some random number = 20\r\nEnter some random number = 50\r\nThe modified Id is: 50<\/pre>\n
\n