{"id":4870,"date":"2021-07-14T22:32:12","date_gmt":"2021-07-14T17:02:12","guid":{"rendered":"https:\/\/blog.guvi.in\/?p=4870"},"modified":"2026-01-08T17:11:27","modified_gmt":"2026-01-08T11:41:27","slug":"description-of-scope-in-python-programming-language","status":"publish","type":"post","link":"https:\/\/www.guvi.in\/blog\/description-of-scope-in-python-programming-language\/","title":{"rendered":"The LEGB Rule Explaining Scope in Python Programming Language"},"content":{"rendered":"\n<p>Why understand <strong>Scope in Python<\/strong>? <\/p>\n\n\n\n<p><strong>Readability<\/strong> or <strong>ease of writing<\/strong> doesn\u2019t make a programming language ideal. There are hundreds of elements that come together and frame an ideal programming language. Among those hundreds of elements, the Scope is one of them.<\/p>\n\n\n\n<p>In this write-up, I\u2019ll give you a complete description of Scope in Python. However, Python is one of the easiest programming languages available right now, though it is not perfect. There are a few concepts of Python that could drive you crazy sometimes.&nbsp;<\/p>\n\n\n\n<p>If you are a programmer, the concept of Scope in Python might seem easy to you. However, if you are a beginner in Python programming, Scope in Python can be a nightmare for you!<\/p>\n\n\n\n<p>Since you are reading this, you don\u2019t have to worry, as, by the end of this guide, you won\u2019t have a single doubt regarding Scopes in Python. In this guide, I\u2019ll focus on the following topics:&nbsp;<\/p>\n\n\n\n<ul>\n<li>Definition of Scope<\/li>\n\n\n\n<li>Resolution of Scopes<\/li>\n<\/ul>\n\n\n\n\n\n<h2 class=\"wp-block-heading\">Definition of Scope in Python<\/h2>\n\n\n\n<p>Technically, Scope is a part of a program where an element (variable, function, constant, or object) with a name can be recognized.<\/p>\n\n\n\n<p>For instance, if a variable \u201cx\u201d (hypothetical) is accessible in 5 code statements, then the area of those five statements will be considered as the Scope of the variable \u201cx.\u201d Want to <a href=\"https:\/\/www.guvi.in\/blog\/do-you-know-how-to-create-variables-in-python\/\">learn how to create variables in Python then click here. <\/a><\/p>\n\n\n\n<p>Refer to the variable with its name \u201cx\u201d beyond those five statements. You might not be able to access it because the binding of that name \u201cx\u201d with the variable is only limited to the region of 5 code statements.&nbsp;<\/p>\n\n\n\n<p>Before diving into the next section, ensure you&#8217;re solid on Python essentials from basics to advanced level. If you are looking for a detailed Python career program, you can join <strong>HCL GUVI\u2019s<\/strong><a href=\"https:\/\/www.guvi.in\/zen-class\/python-course\/?utm_source=blog&amp;utm_medium=organic&amp;utm_campaign=description-of-scope-in-python-programming-language\"><strong> Python Career Program<\/strong><\/a><strong> <\/strong>with placement assistance. You will be able to master the Multiple Exceptions, classes, OOPS concepts, dictionary, and many more, and build real-life projects.<\/p>\n\n\n\n<p>Also, if you would like to explore Python through a Self-paced course, try<strong> HCL GUVI\u2019s<\/strong><a href=\"https:\/\/www.guvi.in\/courses\/programming\/python\/?utm_source=blog&amp;utm_medium=organic&amp;utm_campaign=description-of-scope-in-python-programming-language\"><strong> Python Self-Paced course<\/strong><\/a>.<\/p>\n\n\n\n<p>So, this is what the term scope represents in a programming language. If you are still confused, then refer to the program below:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Basic Scope Representation<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/kMF7vz65CCwyblJ9SkkoxSmFzC3krEW6KF4HIPd1_2THRbnrNnSehLXwub0NEvF1h7frZRwMvZTOigO4pr5-A_-PALQQVpjZ5LAerNjst_Zuf6eIi1_cuNZF_V38lns4Oq20Gm4\" alt=\"\" style=\"width:613px;height:330px\" title=\"\"><\/figure>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/qC06CCDQEIx2gGTVpiJUMAA8QTzuNX9YjvfyX1wpdsno4mgApdTzvUK_NDSjYqYkidFTuvCX29W4ujmgs_Ixa9sLdd0cr4sjuqKIca9EWNfWHaTawl6o0kLTQXrvue40ClmFGqQ\" alt=\"\" style=\"width:613px;height:222px\" title=\"\"><\/figure>\n\n\n\n<p>If you are getting confused about the working of the above program, don\u2019t worry, the next section is entirely about scopes and their resolution.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Resolution of Scope in Python<\/h2>\n\n\n\n<p>Different programming languages have different rules for resolving the Scope. Similarly, Python has a LEGB rule. Every character of the term \u201cLEGB\u201d has a specific meaning and represents a particular scope type.<\/p>\n\n\n\n<p><strong>Note:<\/strong> The scopes mentioned below are arranged in the Narrowest to Broadest range.&nbsp;<\/p>\n\n\n\n<ul>\n<li>Local: This Scope is valid for all the defined elements inside a function or a class.<\/li>\n<\/ul>\n\n\n\n<ul>\n<li>Enclosed: Names defined in nested functions or closed functions.&nbsp;<\/li>\n<\/ul>\n\n\n\n<ul>\n<li>Global: Names defined above a Python class or at the top of a module<\/li>\n<\/ul>\n\n\n\n<ul>\n<li>Built-in: All the predefined names in Python directories.&nbsp;<\/li>\n<\/ul>\n\n\n\n<p>If you are confused with the definition, don\u2019t panic! I will explain each one of them in detail in the next section. But before that, have a look at the image depicting the above terms.&nbsp;<\/p>\n\n\n\n<p>So, I have briefed you about the scope resolution rule, i.e., LEGB. Now, let\u2019s have a look at the detailed explanation of name scope from the LEGB Rule.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Local Scope&nbsp;<\/h3>\n\n\n\n<p>This section is about the first name scope of the LEGB rule, i.e., Local Scope.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/rZ6HT2z5kknLy7g7T_nbNkAXis9K7fQZo8keXJbluHXJ7DwXikbRFgZFp_p_qhNXuXD0niCPzr4ZHvAK4lNAL_z0GgfQQKZE5kRN3X7zyODarrPePvQIKvQt252ehfMmu4tEBm0\" alt=\"scope in Python\" style=\"width:646px;height:294px\" title=\"\"><\/figure>\n\n\n\n<p>Here in the above example, In the \u2018<em>display()<\/em>\u2019 function, the \u2018<em>var<\/em>\u2019 variable, defined in the method itself, is invoked. According to the <strong>LEGB rule<\/strong>, the method must look for the Local Scope in the first place, and that\u2019s what is happening in the above display method.&nbsp;<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/K_nvDfxxiIglKKX1IlK88QmF3PspxVtncymk5PaDev8OdIHQUOsOwLdY_6hZeOxTpj3rCoumnvG7F3FIHCwv4w1hztQQScdOCVNGt-g5xs3MltfNKDnPbLxK5aRPG2n6z0habyU\" alt=\"\" style=\"width:613px;height:204px\" title=\"\"><\/figure>\n\n\n\n<p>If the method can\u2019t locate the name in its local Scope, it will look into the enclosed Scope. A detailed illustration is as follows:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Enclosed Scope in Python<\/h3>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/AE_rvCC6wjfpBtcSENAlZVGwl4_yrn3XOZXNiBM6ofX51lQwB4QKLE9-rBEv9LR6jmzgV8S6Nf8SN2pQtlm4E5sQ9PiaKKPGRY1GA1Ml7nG5OiW5CdYtnnwxC1J3XXj-N6xcn0E\" alt=\"enclosed variable \n\" style=\"width:731px;height:330px\" title=\"\"><\/figure>\n\n\n\n<p>In this illustration, the method \u2018<strong>printvar()<\/strong>\u2019 is trying to print the variable \u2018<strong>var<\/strong>.\u2019 As per the scope resolution rule, method \u2018<strong>printvar()<\/strong>\u2019 should look into its local Scope to find the variable \u2018<strong>var<\/strong>.\u2019&nbsp;<\/p>\n\n\n\n<p>In this case, no variable is defined in the local Scope; therefore, the method \u2018<strong>printvar()<\/strong>\u2019 has to look into the enclosed Scope to find the variable that\u2019s how the variable \u2018var\u2019 was located by the \u2018<strong>printvar()<\/strong>\u2019 method.&nbsp;<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/utJRd32sOn_js7rociAKb_dJZGnACVYuI9FIuRRn2thpDD5QMlTlcg8pDXKIeO75p_AKAHBRzi9Q5tS_ZzLFMKFWrlfMflXxG7mHfz9KiqjSOL8RLu_yTJKTlNg48FEx-Gkh5vw\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Global Scope in Python<\/h3>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/D1S0BKoVK6NQY-ZIQmyf0u5rBlSII7ymYD_3GJ1C6ucZWkSreiSqxKaxkIO4OFVX4VvqZDBOnpBSs_piUPSJ9tqIyUgyhMf-bMZnIx6g4rDaXR2VEbdst2qeM61KpPO8s9evht0\" alt=\"\" style=\"width:731px;height:348px\" title=\"\"><\/figure>\n\n\n\n<p>In the above illustration, no variable is defined in method \u2018<strong>display()<\/strong>\u2019 or nested method \u2018<strong>printvar()<\/strong>\u2019; therefore, there is no local or enclosed scope variable; thus, method \u2018<strong>printvar()<\/strong>\u2019 will look for the global scope variable.&nbsp;<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/jMa9gqz0vN-U7WLJ5buHs-W46bMx62uBkDrlbGyJB2dXfMwnCzS4ja0lmX1mhzzI1srMdaG1cj8wyuzLYK1JE6Qcj3HfJD8P4Tz26NG7JZCwt-f6c1jXLZm9GjurkRzWFB9Lb9Y\" alt=\"\" style=\"width:613px;height:204px\" title=\"\"><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Built-in Scope<\/h3>\n\n\n\n<p>If a method can\u2019t locate any global, enclosed, or local variable, it automatically looks for built-in Scope. For instance,<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/65kH6XeaNEwzd6lfElcazQHh_xAURQCsG07DzGLVx8Jvuy8HB5U9XOjFwnFVPG5CLKAueNR6E_hgazolqlbxrSK0JfdrJ5FkMv6Nn6XJ6Cpc6GvqwFpAXNsvKwhFKw1x0rVOFSs\" alt=\"\" style=\"width:613px;height:384px\" title=\"\"><\/figure>\n\n\n\n<p>Here, if you look closely, you\u2019ll find that the method \u2018<strong>printvar()<\/strong>\u2019 is printing the variable \u2018<strong>e<\/strong>,\u2019 but this variable is neither defined in any function nor at the top of the file.&nbsp;<\/p>\n\n\n\n<p>Therefore, the method couldn\u2019t find the \u2018<strong>e<\/strong>\u2019 variable in <strong>local\/enclosed<\/strong> or <strong>global Scope<\/strong>; as the last option, the method will look for the built-in scope names.<\/p>\n\n\n\n<p>Here the \u2018<strong>e<\/strong>\u2019 is imported from the \u2018<strong>math<\/strong>\u2019 module and acts as a built-in scope variable.&nbsp;<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/PyNUigxZ-L03cu46U4-0r1khlh0CJI7NMvZdLBMZ4bYuBtwSGgq_lJ8Kqkw7IaH8WQASyw9dPENNOujq9-BZIP-TIvKrmwe7mK1n8I6QNPlC3GP5uLY06evdOk_O0SkZJQkWoT8\" alt=\"Scope in Python\" style=\"width:613px;height:204px\" title=\"\"><\/figure>\n\n\n\n<p>Kickstart your Programming journey by enrolling in <strong>HCL GUVI\u2019s<\/strong><a href=\"https:\/\/www.guvi.in\/zen-class\/python-course\/?utm_source=blog&amp;utm_medium=organic&amp;utm_campaign=description-of-scope-in-python-programming-language\"><strong> Python Career Program<\/strong><\/a> where you will master technologies like multiple exceptions, classes, OOPS concepts, dictionaries, and many more, and build real-life projects.<\/p>\n\n\n\n<p>Alternatively, if you would like to explore Python through a Self-Paced course, try <strong>HCL GUVI\u2019s<\/strong><a href=\"https:\/\/www.guvi.in\/courses\/programming\/python\/?utm_source=blog&amp;utm_medium=organic&amp;utm_campaign=description-of-scope-in-python-programming-language\"><strong> Python Self Paced course<\/strong><\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Final Notes<\/h2>\n\n\n\n<p>In this guide, you have learned about different scope variables, and LEGB rules for scope resolution. Besides this, you have also learned about \u201c<strong>How methods locate different variables based on their scopes<\/strong>.\u201d<\/p>\n\n\n\n<p>Now, all you have to do is \u201c<strong>Practise it thoroughly<\/strong>.\u201d Looking at the program won\u2019t be as effective as writing them on your own. It\u2019s okay if you run into errors, you can refer to different guides available on the internet for debugging your program.&nbsp;<\/p>\n\n\n\n<p>You may practice Python coding on our award-winning Codekata platform. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><a href=\"https:\/\/www.guvi.in\/code-kata\/\" data-type=\"link\" data-id=\"https:\/\/www.guvi.in\/code-kata\/\">CODEKATA- HCL GUVI's Practice Platform<\/a><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">MCQs related to the Scope in Python<\/h2>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-3 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:100%\">\n<div class=\"wp-block-group has-background is-layout-flow wp-block-group-is-layout-flow\" style=\"background-color:#d6d9da\"><div class=\"wp-block-group__inner-container\">\n<ol>\n<li>Which of the following definitions define global scope?<br>\n<ol>\n<li>Names that are declared at the top of the program, outside the functions.<\/li>\n\n\n\n<li>Every function present in the program can access the Name.&nbsp;<\/li>\n\n\n\n<li>Both a &amp; b<\/li>\n\n\n\n<li>None of the above.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n\n\n\n<p><br>Answer:(c)<\/p>\n<\/div><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-group has-background is-layout-flow wp-block-group-is-layout-flow\" style=\"background-color:#d6d9da\"><div class=\"wp-block-group__inner-container\">\n<ol start=\"2\">\n<li>The Names declared in the local scope can be accessed within the current function only.<br>\n<ol>\n<li>True<\/li>\n\n\n\n<li>False<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n\n\n\n<p><br>Answer:(a)<\/p>\n<\/div><\/div>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-group has-background is-layout-flow wp-block-group-is-layout-flow\" style=\"background-color:#d6d9da\"><div class=\"wp-block-group__inner-container\">\n<ol start=\"3\">\n<li>What will be the output of the following program?<br><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/nA50Gx7PVAXt7exrmdSNnkqx9gyJfWza-g1jUURudvyZ0-8GmAkuf3AH7eAfJKoM3KVpbqIyDPngUaSMNo4eDdxKDIXbBmDOkIY0TC5bdTxFfP9IB7E7hFN722NMoA\" width=\"624\" height=\"317\" alt=\"\" title=\"\"><\/li>\n<\/ol>\n\n\n\n<ol>\n<li>20<\/li>\n\n\n\n<li>22<\/li>\n\n\n\n<li>Run-Time Error<\/li>\n\n\n\n<li>None of these<\/li>\n<\/ol>\n\n\n\n<p>Answer:(b)<\/p>\n<\/div><\/div>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-group has-background is-layout-flow wp-block-group-is-layout-flow\" style=\"background-color:#d6d9da\"><div class=\"wp-block-group__inner-container\">\n<ol start=\"4\">\n<li>If in a variable, a local variable declared in function<em> fx()<\/em> and a global variable have identical names then which variable will the function<em> fx() <\/em>invoke?<br>\n<ol>\n<li>Local Variable<\/li>\n\n\n\n<li>Global Variable<\/li>\n\n\n\n<li>Run-time Error<\/li>\n\n\n\n<li>A null value will be printed<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n\n\n\n<p>Answer:(a)<\/p>\n<\/div><\/div>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-group is-layout-flow wp-block-group-is-layout-flow\"><div class=\"wp-block-group__inner-container\">\n<div class=\"wp-block-group is-layout-flow wp-block-group-is-layout-flow\"><div class=\"wp-block-group__inner-container\">\n<div class=\"wp-block-group has-background is-layout-flow wp-block-group-is-layout-flow\" style=\"background-color:#d6d9da\"><div class=\"wp-block-group__inner-container\">\n<ol start=\"5\">\n<li>Which thumb rule helps to resolve the scopes in Python<br>\n<ol>\n<li>LEGB<\/li>\n\n\n\n<li>LGEB<\/li>\n\n\n\n<li>BGEL<\/li>\n\n\n\n<li>None of the above<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n\n\n\n<p>Answer:(a)<\/p>\n<\/div><\/div>\n<\/div><\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Why understand Scope in Python? Readability or ease of writing doesn\u2019t make a programming language ideal. There are hundreds of elements that come together and frame an ideal programming language. Among those hundreds of elements, the Scope is one of them. In this write-up, I\u2019ll give you a complete description of Scope in Python. However, [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":4872,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37,717],"tags":[],"views":"5363","authorinfo":{"name":"Archana","url":"https:\/\/www.guvi.in\/blog\/author\/archana\/"},"thumbnailURL":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2021\/07\/LEGB-scope-in-python-300x300.png","jetpack_featured_media_url":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2021\/07\/LEGB-scope-in-python-e1626282597854.png","_links":{"self":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/4870"}],"collection":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/comments?post=4870"}],"version-history":[{"count":17,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/4870\/revisions"}],"predecessor-version":[{"id":98656,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/4870\/revisions\/98656"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media\/4872"}],"wp:attachment":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media?parent=4870"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/categories?post=4870"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/tags?post=4870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}