What are the basic scoping rules for python variables?



Variables are classified into Global variables and Local variables based on their scope. The main difference between Global and local variables is that global variables can be accessed globally in the entire program, whereas local variables can be accessed only within the function or block in which they are defined.

Local variables are those that are defined inside a function but have a scope that is only applicable to that function, as opposed to global variables, which are defined outside of any function and have a global scope. In other words, we may argue that although global variables are accessible across the program and inside all functions, local variables are only accessible within the function in which they were initialized. Local variables are variables that are created within a function and are exclusive to that function. It can't be accessible outside of the function anyplace. Let's examine the creation of a local variable.

Local Variables in Python

A local variable is a specific kind of variable that can be utilized when the method or statement block in which it is declared defines the variable's scope and extend. The for each statement uses it as an iteration variable, the specific-catch clause uses it as an exception variable, and the using statement uses it as a resource variable.

In the method or statement block where it is declared, it can also be used as a constant whose value cannot be changed. Language integrated queries (LINQ), which return anonymous types, can be dealt with by using an implicitly typed local variable whose type is deduced by the compiler from the expression to its right. This allows a custom type to be created for each LINQ result set.

Example

Let’s look at an example to create a local variable.

def f(): #local variable L = "Hello World" print(L) # Driver code f()

Output

The output is as follows.

Hello World

Global Variables

Only when assigning values to or changing global variables within a function do we need to use the global keyword. For printing and accessing, global is not necessary. Due to the assignment to s inside of f(), Python "assumes" that we want a local variable, which is why the first sentence throws the error. If a variable is modified or created inside of a function without being defined as a global variable, it is considered local.

Example

The following is an example of making a global variable and accessing it.

pi = 3.142 radius = 8 def circle(): #radius is a global varible global radius radius = radius * 2 area_of_circle = pi * (radius) ** 2 print("The area of circle is: ", area_of_circle) circle()

Output

The output produced is as follows.

The area of circle is:  804.352

Enclosing or Nonlocal Scope

The variable that is specified in the nested function is known as Nonlocal Variable. It indicates that the variable cannot be both local and global in scope. The nonlocal keyword is used to generate nonlocal variables. The inner function is nested inside the outer function that we generated in the following code (). Inner() function is defined in the outer() function's scope. Changes made to the nonlocal variable declared in the inner() function are mirrored in the output of the outer function.

Example

The following example demonstrates the usage

def Outer(): x = "local" def Inner(): nonlocal x x = "nonlocal" print("inner:", x) Inner() print("outer:", x) Outer()

Output

The output is as follows.

inner: nonlocal
outer: nonlocal

Advertisements