{"id":10729,"date":"2021-04-27T12:18:21","date_gmt":"2021-04-27T06:48:21","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=10729"},"modified":"2021-04-27T12:20:02","modified_gmt":"2021-04-27T06:50:02","slug":"python-globals","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/python-globals\/","title":{"rendered":"4 Examples to Use Python globals() Function"},"content":{"rendered":"\n<p>In python, we have discussed many concepts and conversions. But sometimes, we come to a situation where we need to return the dictionary according to the current global symbol table. In this tutorial, we will be discussing the concept of the <strong>python globals() function,<\/strong> which is used to updates and returns a dictionary of the current global symbol table.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_74 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #990303;color:#990303\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #990303;color:#990303\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.pythonpool.com\/python-globals\/#What_is_globals_function\" >What is globals() function?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.pythonpool.com\/python-globals\/#Syntax\" >Syntax<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.pythonpool.com\/python-globals\/#Parameter\" >Parameter<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.pythonpool.com\/python-globals\/#Return_Value\" >Return Value<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.pythonpool.com\/python-globals\/#Examples_Demystifying_Python_globals_Function_in_Python\" >Examples Demystifying  Python globals() Function in Python<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.pythonpool.com\/python-globals\/#1_Using_Globals_function_in_Python\" >1. Using Globals() function in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.pythonpool.com\/python-globals\/#2_Python_globals_function_program_with_function\" >2. Python globals() function program with function<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.pythonpool.com\/python-globals\/#3_Modifying_global_variable_using_the_function_in_Python\" >3. Modifying global variable using the function in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.pythonpool.com\/python-globals\/#4_Using_globals_inside_a_class_in_Python\" >4. Using globals() inside a class in Python<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.pythonpool.com\/python-globals\/#Difference_between_locals_globals_and_vars_function_in_Python\" >Difference between locals(), globals() and vars() function in Python<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.pythonpool.com\/python-globals\/#locals_function\" >locals() function<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.pythonpool.com\/python-globals\/#globals_function\" >globals() function<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.pythonpool.com\/python-globals\/#vars_function\" >vars() function<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/www.pythonpool.com\/python-globals\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/www.pythonpool.com\/python-globals\/#FAQs\" >FAQs<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/www.pythonpool.com\/python-globals\/#1_What_does_globals_do_in_Python\" >1. What does globals () do in Python?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/www.pythonpool.com\/python-globals\/#2_How_to_modify_global_variables_using_globals\" >2. How to modify global variables using&nbsp;globals()?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"h-what-is-globals-function\"><span class=\"ez-toc-section\" id=\"What_is_globals_function\"><\/span>What is globals() function?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><strong>Python globals() function is a built-in function used to update and return a dictionary of the current global symbol table.<\/strong> The functions and variables that are not associated with any class or function are stored globally. It is mostly used for debugging purposes to see what objects the global scope actually contains.<\/p>\n\n\n\n<p><strong>Symbol Table<\/strong>: A symbol table is a type of data structure maintained or created by a compiler used to store all necessary information about the program. These include all the variable names, methods, classes, etc.<\/p>\n\n\n\n<p><strong>Global Symbol Table<\/strong>: The table is used to store all the information needed for the program&#8217;s global scope, and this information can be accessed using the built-in <strong>globals() function.<\/strong> <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-syntax\"><span class=\"ez-toc-section\" id=\"Syntax\"><\/span>Syntax<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The global table dictionary is the dictionary of the current module where inside a function, this is a module where it is defined, not the module where it is called.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>globals()<\/strong><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-parameter\"><span class=\"ez-toc-section\" id=\"Parameter\"><\/span>Parameter<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>It does not take any parameter.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-return-value\"><span class=\"ez-toc-section\" id=\"Return_Value\"><\/span>Return Value<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>It returns the dictionary associated with the current Global symbol table.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-examples-demystifying-python-globals-function-in-python\"><span class=\"ez-toc-section\" id=\"Examples_Demystifying_Python_globals_Function_in_Python\"><\/span>Examples Demystifying  Python globals() Function in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Here, we will be discussing all the examples of the built in globals() function in python:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-1-using-globals-function-in-python\"><span class=\"ez-toc-section\" id=\"1_Using_Globals_function_in_Python\"><\/span>1. Using Globals() function in Python<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>In this example, we will try to print the given function directly with the help of the print function in python. <strong>globals() function will directly paste the Local Symbol Table information on the console, on that scope where globals() was called from!<\/strong>. Let us look at the example for understanding the concept in detail.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#print() function and globals() function\nprint(globals())\n<\/pre><\/div>\n\n\n<p class=\"has-medium-font-size\"><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"918\" height=\"117\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/1-6.png\" alt=\"Using Globals in Python\" class=\"wp-image-10734\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/1-6.png 918w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/1-6-300x38.png 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/1-6-768x98.png 768w\" sizes=\"(max-width: 918px) 100vw, 918px\" \/><\/figure>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\" id=\"block-36ea5adb-b066-4869-9ae7-abb901f4841b\"><li>Here, we will directly print the given function with the help of the print function.<\/li><li>The given function will directly paste the Global Symbol Table information on the console, on that scope where globals() was called from!<\/li><li>Hence, we obtain the required dictionary.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-2-python-globals-function-program-with-function\"><span class=\"ez-toc-section\" id=\"2_Python_globals_function_program_with_function\"><\/span>2. Python globals() function program with function<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>In this example, we will be using the global variable and then declare the function. Inside of the function, we will try to modify the value using the global variable. Then, we will call the globals() function and print the modified value. Let us look at the example for understanding the concept in detail.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# global variable\nnum = 15\n\ndef func():\n\ta = 10\n\tb = a + num\n\t\n\t# Calling globals()\n\tglobals()&#x5B;'num'] = b\n\tprint (b)\n\t\nfunc()\n<\/pre><\/div>\n\n\n<p class=\"has-medium-font-size\"><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"909\" height=\"78\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/3-2.png\" alt=\"Python globals() function program with function\" class=\"wp-image-10732\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/3-2.png 909w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/3-2-300x26.png 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/3-2-768x66.png 768w\" sizes=\"(max-width: 909px) 100vw, 909px\" \/><\/figure>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Firstly, we will be taking the global variable as num equal to 15.<\/li><li>Then, we will be declaring a function with func().<\/li><li>Inside, which we have modified the value of some variable by using a global variable.<\/li><li>After that, we will be calling the globals() function.<\/li><li>At last, we will print the updated value of the variable of b.<\/li><li>Hence, you can see the modified value.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-3-modifying-global-variable-using-the-function-in-python\"><span class=\"ez-toc-section\" id=\"3_Modifying_global_variable_using_the_function_in_Python\"><\/span>3. Modifying global variable using the function in Python<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>In this example, we will be using the global variable and then declare the function. Inside of the function, we will try to modify the value using the global variable. Then, we will call the globals() function and print the modified value. Let us look at the example for understanding the concept in detail.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#input number\nnumber = 52\n\n#applying globals() function\nglobals()&#x5B;'number'] = 25\nprint('The number is:', number)\n<\/pre><\/div>\n\n\n<p class=\"has-medium-font-size\"><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"900\" height=\"75\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/2-1.png\" alt=\" Modifying global variable using the function in python\" class=\"wp-image-10733\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/2-1.png 900w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/2-1-300x25.png 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/2-1-768x64.png 768w\" sizes=\"(max-width: 900px) 100vw, 900px\" \/><\/figure>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\" id=\"block-06991cd2-61d6-4d5c-aaa2-6f2311de0162\"><li>Firstly, we will be taking the global variable as a number equal to 52.<\/li><li>After that, we will be calling the globals() function.<\/li><li>At last, we will print the updated value of the variable of number.<\/li><li>Hence, you can see the modified value of the number variable.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-4-using-globals-inside-a-class-in-python\"><span class=\"ez-toc-section\" id=\"4_Using_globals_inside_a_class_in_Python\"><\/span>4. Using globals() inside a class in Python<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>In this example, the globals() function is used inside the class. We have assigned some integer values to the variable num. Then, we have called the class with the name python, and inside it printed the globals(). Let us look at the example for understanding the concept in detail.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnum = 100\nclass python:\n    print('\\nglobals() value inside and outside class\\n', globals())\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"903\" height=\"142\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/10.png\" alt=\"Using globals() inside a class\" class=\"wp-image-10811\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/10.png 903w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/10-300x47.png 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/10-768x121.png 768w\" sizes=\"(max-width: 903px) 100vw, 903px\" \/><\/figure>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Firstly, we have assigned an integer value to the num variable.<\/li><li>Then, we have called the class with the name python.<\/li><li>Inside the class, printed the globals().<\/li><li>Hence, we have seen the output.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-difference-between-locals-globals-and-vars-function-in-python\"><span class=\"ez-toc-section\" id=\"Difference_between_locals_globals_and_vars_function_in_Python\"><\/span>Difference between locals(), globals() and vars() function in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-locals-function\"><span class=\"ez-toc-section\" id=\"locals_function\"><\/span>locals() function<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p><strong><a href=\"http:\/\/www.pythonpool.com\/python-locals\/\" target=\"_blank\" rel=\"noreferrer noopener\"><span style=\"text-decoration: underline;\">Python locals() function<\/span><\/a> is an in-built<\/strong> <strong>function used to update and return a current local symbol table dictionary.<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-globals-function\"><span class=\"ez-toc-section\" id=\"globals_function\"><\/span>globals() function<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p><strong>Python globals() function is an in-built<\/strong> <strong>function used to update and return a current global symbol table dictionary.<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-vars-function\"><span class=\"ez-toc-section\" id=\"vars_function\"><\/span>vars() function<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p><strong><a href=\"http:\/\/www.pythonpool.com\/python-vars\/\" target=\"_blank\" rel=\"noreferrer noopener\"><span style=\"text-decoration: underline;\">Python vars() function<\/span><\/a> is an in-built function that is used to return a dictionary of the current namespace or the dictionary of argument.<\/strong> The main difference between <a href=\"http:\/\/www.pythonpool.com\/python-vars\/\" target=\"_blank\" rel=\"noreferrer noopener\"><span style=\"text-decoration: underline;\">vars()<\/span><\/a> and locals() is that <a href=\"http:\/\/www.pythonpool.com\/python-vars\/\" target=\"_blank\" rel=\"noreferrer noopener\"><span style=\"text-decoration: underline;\">vars()<\/span><\/a> can take an argument and return a dictionary for the requested object.<\/p>\n\n\n\n<p>If we write the <a href=\"http:\/\/www.pythonpool.com\/python-locals\/\" target=\"_blank\" rel=\"noreferrer noopener\"><span style=\"text-decoration: underline;\">locals()<\/span><\/a> function and globals() function in the global environment, they will result in the same output. So, we will be taking an example with the help of function.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef func():\n    num = 31\n    print (&quot;Locals : &quot;, locals())\n    print (&quot;Vars : &quot;, vars())\n    print (&quot;Globals : &quot;, globals())\nfunc()\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"922\" height=\"201\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/7-1.png\" alt=\"vars() function\" class=\"wp-image-10758\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/7-1.png 922w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/7-1-300x65.png 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/7-1-768x167.png 768w\" sizes=\"(max-width: 922px) 100vw, 922px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this tutorial, we have discussed the concept of the built-in globals() function in python. We have seen what the globals() function, <a href=\"https:\/\/docs.python.org\/3\/library\/symtable.html\" target=\"_blank\" rel=\"noreferrer noopener\">symbol table<\/a>, and global symbol table is. Then, we have explained to you the syntax, parameter, and return value of the function. At last, we have explained the concept in detail with the help of different examples. All the examples are explained in detail with the help of examples. You can use any of the functions according to your choice and your requirement in the program.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-faqs\"><span class=\"ez-toc-section\" id=\"FAQs\"><\/span>FAQs<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-1-what-does-globals-do-in-python\"><span class=\"ez-toc-section\" id=\"1_What_does_globals_do_in_Python\"><\/span>1. What does globals () do in Python?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The globals() function in python is an in-built function that is used to return a dictionary of the current global symbol table. A symbol table contains all the important details about the program.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-2-how-to-modify-global-variables-using-globals\"><span class=\"ez-toc-section\" id=\"2_How_to_modify_global_variables_using_globals\"><\/span>2. How to modify global variables using&nbsp;globals()?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>We can easily modify the global variables with the help of the globals() function python. Let us see the code snippet through which you can understand the modification very nicely.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nnum = 20\nglobals()&#x5B;'num'] = 55\nprint('The number is:', num)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The number is: 55<\/code><\/pre>\n\n\n\n<p>As you can see in the code, the num variable is set with the value of 20. Then, we have applied the globals() function and changed the value of the num value to 55, and at last, you can see the output as 55. Through this, we can easily modify the global variable.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In python, we have discussed many concepts and conversions. But sometimes, we come to a situation where we need to return the dictionary according to &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"4 Examples to Use Python globals() Function\" class=\"read-more button\" href=\"https:\/\/www.pythonpool.com\/python-globals\/#more-10729\" aria-label=\"More on 4 Examples to Use Python globals() Function\">Read more<\/a><\/p>\n","protected":false},"author":17,"featured_media":10857,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[15],"tags":[3890,3891,3893,3894,3892],"class_list":["post-10729","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-globals-in-python","tag-globals-python","tag-globals-python-2","tag-python-globals-2","tag-python-globals","infinite-scroll-item"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.1 (Yoast SEO v25.0) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>4 Examples to Use Python globals() Function - Python Pool<\/title>\n<meta name=\"description\" content=\"Python globals() function is an in-built function that is used to updates and return a dictionary of the current global symbol table.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pythonpool.com\/python-globals\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"4 Examples to Use Python globals() Function\" \/>\n<meta property=\"og:description\" content=\"In python, we have discussed many concepts and conversions. But sometimes, we come to a situation where we need to return the dictionary according to the\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonpool.com\/python-globals\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Pool\" \/>\n<meta property=\"article:published_time\" content=\"2021-04-27T06:48:21+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-04-27T06:50:02+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/Examples-to-Use-Python-globals-Function.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1350\" \/>\n\t<meta property=\"og:image:height\" content=\"650\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Siddharth Jain\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:site\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Siddharth Jain\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.pythonpool.com\/python-globals\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-globals\/\"},\"author\":{\"name\":\"Siddharth Jain\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/75a3240fabe5ea90200777a9d8d3b4fa\"},\"headline\":\"4 Examples to Use Python globals() Function\",\"datePublished\":\"2021-04-27T06:48:21+00:00\",\"dateModified\":\"2021-04-27T06:50:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-globals\/\"},\"wordCount\":1038,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-globals\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/Examples-to-Use-Python-globals-Function.jpg\",\"keywords\":[\"globals in python\",\"globals python\",\"globals() python\",\"python globals\",\"python globals()\"],\"articleSection\":[\"Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.pythonpool.com\/python-globals\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.pythonpool.com\/python-globals\/\",\"url\":\"https:\/\/www.pythonpool.com\/python-globals\/\",\"name\":\"4 Examples to Use Python globals() Function - Python Pool\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-globals\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-globals\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/Examples-to-Use-Python-globals-Function.jpg\",\"datePublished\":\"2021-04-27T06:48:21+00:00\",\"dateModified\":\"2021-04-27T06:50:02+00:00\",\"description\":\"Python globals() function is an in-built function that is used to updates and return a dictionary of the current global symbol table.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-globals\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.pythonpool.com\/python-globals\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/python-globals\/#primaryimage\",\"url\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/Examples-to-Use-Python-globals-Function.jpg\",\"contentUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/Examples-to-Use-Python-globals-Function.jpg\",\"width\":1350,\"height\":650,\"caption\":\"Python globals\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.pythonpool.com\/python-globals\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.pythonpool.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"4 Examples to Use Python globals() Function\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.pythonpool.com\/#website\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"name\":\"Python Pool\",\"description\":\"Your One-Stop Python Learning Destination\",\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.pythonpool.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.pythonpool.com\/#organization\",\"name\":\"Python Pool\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"contentUrl\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"width\":452,\"height\":185,\"caption\":\"Python Pool\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/pythonpool\",\"https:\/\/www.youtube.com\/c\/pythonpool\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/75a3240fabe5ea90200777a9d8d3b4fa\",\"name\":\"Siddharth Jain\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/152b0ac2e5fa2e6328f374499fff4a7a6299477b9cf7bbb15ebc01a88f8f673f?s=96&d=wavatar&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/152b0ac2e5fa2e6328f374499fff4a7a6299477b9cf7bbb15ebc01a88f8f673f?s=96&d=wavatar&r=g\",\"caption\":\"Siddharth Jain\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"4 Examples to Use Python globals() Function - Python Pool","description":"Python globals() function is an in-built function that is used to updates and return a dictionary of the current global symbol table.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pythonpool.com\/python-globals\/","og_locale":"en_US","og_type":"article","og_title":"4 Examples to Use Python globals() Function","og_description":"In python, we have discussed many concepts and conversions. But sometimes, we come to a situation where we need to return the dictionary according to the","og_url":"https:\/\/www.pythonpool.com\/python-globals\/","og_site_name":"Python Pool","article_published_time":"2021-04-27T06:48:21+00:00","article_modified_time":"2021-04-27T06:50:02+00:00","og_image":[{"width":1350,"height":650,"url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/Examples-to-Use-Python-globals-Function.jpg","type":"image\/jpeg"}],"author":"Siddharth Jain","twitter_card":"summary_large_image","twitter_creator":"@pythonpool","twitter_site":"@pythonpool","twitter_misc":{"Written by":"Siddharth Jain","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonpool.com\/python-globals\/#article","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/python-globals\/"},"author":{"name":"Siddharth Jain","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/75a3240fabe5ea90200777a9d8d3b4fa"},"headline":"4 Examples to Use Python globals() Function","datePublished":"2021-04-27T06:48:21+00:00","dateModified":"2021-04-27T06:50:02+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-globals\/"},"wordCount":1038,"commentCount":0,"publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-globals\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/Examples-to-Use-Python-globals-Function.jpg","keywords":["globals in python","globals python","globals() python","python globals","python globals()"],"articleSection":["Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonpool.com\/python-globals\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pythonpool.com\/python-globals\/","url":"https:\/\/www.pythonpool.com\/python-globals\/","name":"4 Examples to Use Python globals() Function - Python Pool","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-globals\/#primaryimage"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-globals\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/Examples-to-Use-Python-globals-Function.jpg","datePublished":"2021-04-27T06:48:21+00:00","dateModified":"2021-04-27T06:50:02+00:00","description":"Python globals() function is an in-built function that is used to updates and return a dictionary of the current global symbol table.","breadcrumb":{"@id":"https:\/\/www.pythonpool.com\/python-globals\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonpool.com\/python-globals\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/python-globals\/#primaryimage","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/Examples-to-Use-Python-globals-Function.jpg","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/Examples-to-Use-Python-globals-Function.jpg","width":1350,"height":650,"caption":"Python globals"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonpool.com\/python-globals\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonpool.com\/"},{"@type":"ListItem","position":2,"name":"4 Examples to Use Python globals() Function"}]},{"@type":"WebSite","@id":"https:\/\/www.pythonpool.com\/#website","url":"https:\/\/www.pythonpool.com\/","name":"Python Pool","description":"Your One-Stop Python Learning Destination","publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pythonpool.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.pythonpool.com\/#organization","name":"Python Pool","url":"https:\/\/www.pythonpool.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/","url":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","contentUrl":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","width":452,"height":185,"caption":"Python Pool"},"image":{"@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/pythonpool","https:\/\/www.youtube.com\/c\/pythonpool"]},{"@type":"Person","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/75a3240fabe5ea90200777a9d8d3b4fa","name":"Siddharth Jain","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/152b0ac2e5fa2e6328f374499fff4a7a6299477b9cf7bbb15ebc01a88f8f673f?s=96&d=wavatar&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/152b0ac2e5fa2e6328f374499fff4a7a6299477b9cf7bbb15ebc01a88f8f673f?s=96&d=wavatar&r=g","caption":"Siddharth Jain"}}]}},"_links":{"self":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/10729","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/comments?post=10729"}],"version-history":[{"count":21,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/10729\/revisions"}],"predecessor-version":[{"id":10859,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/10729\/revisions\/10859"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media\/10857"}],"wp:attachment":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media?parent=10729"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/categories?post=10729"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/tags?post=10729"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}