{"id":4448,"date":"2023-11-27T12:27:17","date_gmt":"2023-11-27T12:27:17","guid":{"rendered":"https:\/\/ibex.tech\/python\/?p=4448"},"modified":"2023-11-30T11:28:29","modified_gmt":"2023-11-30T11:28:29","slug":"global","status":"publish","type":"post","link":"https:\/\/ibex.tech\/python\/memory-python\/global","title":{"rendered":"Global"},"content":{"rendered":"\n<p>Variables and objects created outside of a function are global and can be read (not written) by any function<\/p>\n\n\n\n<p>The global keyword can be used to create a global object inside a function.<\/p>\n\n\n\n<p>The global keyword must be used inside a function if it wants to change the value of a global object inside the function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db1 = None        #&lt;&lt;&lt;This is a global object\n\n\ndef my_fucntion1():\n    global db1        #Must be declared with \"global\" here because we are going to change its value\n    \n    db1 = sqlite3.connect(\"my_database\")\n\n\ndef my_fucntion2():\n    #global db1        &lt;&lt;&lt;No need to declare it here because we are only reading it\n    \n    db1.commit()\n <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Variables and objects created outside of a function are global and can be read (not written) by any function The global keyword can be used to create a global object inside a function. The global keyword must be used inside a function if it wants to change the value of a global object inside the [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[],"class_list":["post-4448","post","type-post","status-publish","format-standard","hentry","category-memory-python"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4448","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/comments?post=4448"}],"version-history":[{"count":1,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4448\/revisions"}],"predecessor-version":[{"id":4449,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4448\/revisions\/4449"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/media?parent=4448"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/categories?post=4448"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/tags?post=4448"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}