{"id":530,"date":"2023-10-30T15:33:01","date_gmt":"2023-10-30T12:33:01","guid":{"rendered":"https:\/\/www.facadium.com.tr\/blog\/?p=530"},"modified":"2023-10-30T15:33:01","modified_gmt":"2023-10-30T12:33:01","slug":"pythonda-global-ve-lokal-degiskenler","status":"publish","type":"post","link":"https:\/\/www.facadium.com.tr\/blog\/pythonda-global-ve-lokal-degiskenler\/","title":{"rendered":"Python&#8217;da Global ve Lokal De\u011fi\u015fkenler"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Kapsam ve Kullan\u0131m<\/h2>\n\n\n\n<p>Python programlama dilinde, de\u011fi\u015fkenlerin kapsam\u0131 (scope) olduk\u00e7a \u00f6nemlidir. Kapsam, bir de\u011fi\u015fkenin hangi kod blo\u011funda veya fonksiyonda eri\u015filebilir oldu\u011funu ve kullan\u0131labilece\u011fini belirler. Python&#8217;da iki temel kapsam t\u00fcr\u00fc vard\u0131r: global ve lokal kapsam.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Global De\u011fi\u015fkenler<\/h3>\n\n\n\n<p>Global de\u011fi\u015fkenler, program\u0131n herhangi bir yerinde tan\u0131mlanm\u0131\u015f ve kullan\u0131labilen de\u011fi\u015fkenlerdir. Bu de\u011fi\u015fkenlere herhangi bir kod blo\u011funda veya fonksiyonda eri\u015filebilir. Global de\u011fi\u015fkenleri tan\u0131mlarken, bunlar\u0131 genellikle program\u0131n en \u00fcst d\u00fczeyinde veya fonksiyonlar\u0131n d\u0131\u015f\u0131nda tan\u0131mlars\u0131n\u0131z.<\/p>\n\n\n\n<p>\u0130\u015fte bir \u00f6rnek:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Global bir de\u011fi\u015fken tan\u0131mlama\r\nglobal_degisken = 10\r\n\r\ndef fonksiyon():\r\n    # Global de\u011fi\u015fkene eri\u015fme ve kullanma\r\n    print(\"Global De\u011fi\u015fken:\", global_degisken)\r\n\r\nfonksiyon()  # Fonksiyonu \u00e7a\u011f\u0131rma\r\nprint(\"Global De\u011fi\u015fken (D\u0131\u015far\u0131da):\", global_degisken)\r<\/code><\/pre>\n\n\n\n<p>Yukar\u0131daki \u00f6rnekte, &#8220;global_degisken&#8221; adl\u0131 bir global de\u011fi\u015fken tan\u0131mland\u0131. Bu de\u011fi\u015fkene herhangi bir yerden eri\u015filebilir ve kullan\u0131labilir.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Lokal De\u011fi\u015fkenler<\/h3>\n\n\n\n<p>Lokal de\u011fi\u015fkenler, yaln\u0131zca tan\u0131mland\u0131klar\u0131 kod blo\u011funda veya fonksiyonda eri\u015filebilir ve kullan\u0131labilirler. Bir fonksiyon i\u00e7inde tan\u0131mlanan de\u011fi\u015fkenler, yaln\u0131zca o fonksiyon i\u00e7inde ge\u00e7erlidir.<\/p>\n\n\n\n<p>\u0130\u015fte bir \u00f6rnek:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def fonksiyon():\r\n    # Lokal bir de\u011fi\u015fken tan\u0131mlama\r\n    lokal_degisken = 20\r\n    print(\"Lokal De\u011fi\u015fken:\", lokal_degisken)\r\n\r\nfonksiyon()  # Fonksiyonu \u00e7a\u011f\u0131rma\r\n\r\n# Lokal de\u011fi\u015fken d\u0131\u015far\u0131da eri\u015filemez\r\nprint(\"Lokal De\u011fi\u015fken (D\u0131\u015far\u0131da):\", lokal_degisken)  # Hata!\r<\/code><\/pre>\n\n\n\n<p>Yukar\u0131daki \u00f6rnekte, &#8220;lokal_degisken&#8221; adl\u0131 bir lokal de\u011fi\u015fken, &#8220;fonksiyon&#8221; i\u00e7inde tan\u0131mland\u0131. Bu de\u011fi\u015fken yaln\u0131zca &#8220;fonksiyon&#8221; i\u00e7inde eri\u015filebilir ve d\u0131\u015far\u0131da kullan\u0131ld\u0131\u011f\u0131nda hata verir.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Global ve Lokal De\u011fi\u015fkenleri Kullanma<\/h3>\n\n\n\n<p>Bir fonksiyon i\u00e7inde global bir de\u011fi\u015fkene eri\u015fmek veya global bir de\u011fi\u015fkeni de\u011fi\u015ftirmek isterseniz, &#8220;global&#8221; anahtar kelimesini kullanabilirsiniz. \u0130\u015fte bir \u00f6rnek:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>global_degisken = 10\r\n\r\ndef fonksiyon():\r\n    global global_degisken  # global de\u011fi\u015fkeni tan\u0131mla\r\n    global_degisken = 30  # global de\u011fi\u015fkenin de\u011ferini de\u011fi\u015ftir\r\n    print(\"Fonksiyon \u0130\u00e7inde Global De\u011fi\u015fken:\", global_degisken)\r\n\r\nfonksiyon()  # Fonksiyonu \u00e7a\u011f\u0131rma\r\nprint(\"D\u0131\u015far\u0131da Global De\u011fi\u015fken:\", global_degisken)\r<\/code><\/pre>\n\n\n\n<p>Yukar\u0131daki \u00f6rnekte, &#8220;fonksiyon&#8221; i\u00e7inde &#8220;global global_degisken&#8221; ifadesi kullanarak global bir de\u011fi\u015fkeni tan\u0131mlad\u0131k ve de\u011ferini de\u011fi\u015ftirdik.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u00d6zet<\/h3>\n\n\n\n<p>Python&#8217;da global de\u011fi\u015fkenler program\u0131n herhangi bir yerinden eri\u015filebilir ve kullan\u0131labilirken, lokal de\u011fi\u015fkenler yaln\u0131zca tan\u0131mland\u0131klar\u0131 kod blo\u011funda veya fonksiyonda ge\u00e7erlidir. Global de\u011fi\u015fkenlere herhangi bir yerden eri\u015fmek m\u00fcmk\u00fcnd\u00fcr ve bu de\u011fi\u015fkenler program\u0131n herhangi bir b\u00f6l\u00fcm\u00fcnde kullan\u0131labilir. Lokal de\u011fi\u015fkenler ise tan\u0131mland\u0131klar\u0131 fonksiyon veya kod blo\u011fu i\u00e7inde ge\u00e7erli olur. Global de\u011fi\u015fkenlere bir fonksiyon i\u00e7inde eri\u015fmek veya de\u011fi\u015ftirmek i\u00e7in &#8220;global&#8221; anahtar kelimesi kullan\u0131labilir.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Kapsam ve Kullan\u0131m Python programlama dilinde, de\u011fi\u015fkenlerin kapsam\u0131 (scope) olduk\u00e7a \u00f6nemlidir. Kapsam, bir de\u011fi\u015fkenin hangi kod blo\u011funda veya fonksiyonda eri\u015filebilir oldu\u011funu ve kullan\u0131labilece\u011fini belirler. Python&#8217;da [&#8230;]<\/p>\n","protected":false},"author":3,"featured_media":531,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[8,9],"class_list":["post-530","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python","tag-yazilim"],"_links":{"self":[{"href":"https:\/\/www.facadium.com.tr\/blog\/wp-json\/wp\/v2\/posts\/530","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.facadium.com.tr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.facadium.com.tr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.facadium.com.tr\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.facadium.com.tr\/blog\/wp-json\/wp\/v2\/comments?post=530"}],"version-history":[{"count":1,"href":"https:\/\/www.facadium.com.tr\/blog\/wp-json\/wp\/v2\/posts\/530\/revisions"}],"predecessor-version":[{"id":532,"href":"https:\/\/www.facadium.com.tr\/blog\/wp-json\/wp\/v2\/posts\/530\/revisions\/532"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.facadium.com.tr\/blog\/wp-json\/wp\/v2\/media\/531"}],"wp:attachment":[{"href":"https:\/\/www.facadium.com.tr\/blog\/wp-json\/wp\/v2\/media?parent=530"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.facadium.com.tr\/blog\/wp-json\/wp\/v2\/categories?post=530"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.facadium.com.tr\/blog\/wp-json\/wp\/v2\/tags?post=530"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}