A Quick Guide To Python Scope
Published: 2021-01-25 22:28:28

Different programming language has its own scope and how to define it. Working with python Scope could be somehow confusing especially to beginners, so let’s quickly go through the key rules to Python’s Scope.

Before we start, have this at the back of your mind that Python Scope follows the LEGB Rule which are:

1.Local: Variable names defined anywhere within a function (def or Lambda), and not defined global in that function.

2.Enclosing Function locals: Variable name in the local scope of a function (def or lambda), from inner to outer.

3.Global: Variable names declared at the top level of a module, or declared global def within a file.

4.Built-in: Names preassigned in the built-in name modules such as range, Syntax Error…

What do all these actually mean?
 If you actually declare a variable name in your code and later in your code, you want to call the variable name, Python needs to look at what is called namespace- that is, it needs to know what variable you’re actually calling and the order goes thus:

It first checks if it’s Local, follow by EFL, then to Global and finally if it still can’t find the namespace, it checks the built-in.

Enough of the theory, let’s walk through some simple examples to make Python’s Scope much more clearer.

To begin with the experiment on scope, imagine we declare a variable y with a value of 40 at the top of the file as shown below, and then let’s create a function that doesn’t accept any parameters and then reset the value of y=20 and also return it.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/d60l8ym2w6r5y4pwgg8t.PNG)

Now let’s go an inch further, imagine we try to call the function and then print the value of y. The output will be 40, by this time I guess you’re wondering why it's not 20. That’s Python Scope in action. Just calm down I will explain it in a jiffy!

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/kddmpsa2gtuybkic8zqh.PNG)

When we reassigned the value of y to be 20 inside the function, that reassignment is limited to the scope of that function - meaning that it doesn’t do the reassignment at the global space, so when I call the value of y. The global y will be displayed. Make sense?

Remember I made mentioned that Python Scope follows the LEGB rule, now let’s talk about it for you to understand how Python Scope really works.
Let’s start with an example which starts with the first scope which is the Local scope.

The variable y declared within the Lambda function is limited to that function and that’s a local level scope.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/enfefsv3k3jd425zxvka.PNG)

Enclosing function locals
In simple term, this is just a function within another function.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/usy1457ec1hlgogpsira.PNG)

As shown above, when we tried to call the greet function nothing will be displayed due to the fact that we only re-assigned the value of name, but when we called the hello function what do you think will be displayed? “ Hello bernard”, right?

That was because the hello function was inside the greet function; and when the programme executes, it first checks its local scope. If we don’t have any name declared within the function it will check if it exists at the global scope and if it does, it will be displayed.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/d0oka9f2v69ncpraokoi.PNG)

Built-in level Scope
These are just built-in scope such as len, Syntax-Error and the like.
Try to type len into your interpreter, anything that pops up is a built-in function and you don’t want that to change (They should never be overwritten!)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/eglbrlni58swrmgxg17k.PNG)

Point to note is that there is more to Python Scope, feel free to explore on your own and know how to use Scope. Thank you for reading.