Python Variable Scope | Types of Variable Scope in Python

Here, we have discussed about the variable scope in python programming language. Scope is a region from where we can access the variable. In simple...

In this article you will learn about the variable scope in python Programming language.

Python Variable Scope | Types of Variable Scope in Python

By the end of this article you will have learned about variable scope in Python and types of python variable scope.

What is Python Variable Scope?

Scope is a region from where we can access the variable. In simple words, that part of a code where the variable is visible in python and from where we can use it when it is required is known as scope of variable. Variable scope is also considered as the lifetime of the variables.

Types of Variable Scope in Python

There are four types of variable scope in python that are explained as follows;

Built-in Scope

In python, all the names which are reserved in built-in modules possess a Built-in Scope and this scope is the widest scope among all. If a identifier is already defined in Built-in scope then we don't have to define it and for that we first have to check local, enclosing and global scope.

x = 14.5

int(x)

print(x)

print(type(x))

In the above example, we don't have to define , and function because it was already defined under built-in scope.

Global Scope

Global scope includes those variables which we can easily access from anywhere in a particular program and we can use it inside any program. And the value of global variable is changeable.

Global Scope Example;

x = 'Python is a programing language'

def myfunc():

  print(x)

myfunc()

print(x)

Output

Python is a programing language

Python is a programing language

Local Scope

The variables which are defined under local scope can be accessed only inside its blocks. 

Local Scope Example;

def myfunc():

  x = 'Python is a programming language.'

  print(x)

myfunc()

Output

Python is a programming language.

Enclosing Scope

Those variables which are not declared under global or local scope comes under the enclosing scope.

def vehicle():

  fun= "Racing Bike"

  def bike():

    model= "KTM Duke 390"

    print(fun)

    print(model)

  bike()

vehicle()

Output

Racing Bike

KTM Duke 390

Now, let's see what happens if we declare the variable with same name in different scopes.

Global And Nonlocal keywords

For the situation, where the same named variable are declared in different scopes, we have Global and Nonlocal keywords;

Global keywords

By using global keyword, we are commanding python to use use only those variable which are globally defined and in order to do that, we just have to write 'global' before variable name.

Global Keyword Example 1;

def myfunc():

  global x

  x = 'Learn Python'

myfunc()

print(x)

Output

Learn Python

Global Keyword Example 2;

x = 'Python'

def myfunc():

  global x

  x = 'Programming Language'

myfunc()

print(x)

Output
Programming Language

Nonlocal keywords

The Nonlocal keywords are used in nested function, where the we need to change Nonlocal variable that is a variable which is not defined under both local as well as global scope.

Nonlocal keywords example; 

print ("Python is easy language: ", end ="") 

def outer(): 

    a = 'Yes'

    def inner(): 

        nonlocal a  

        a = 'No'

    inner() 

    print (a) 

   

outer() 

   

# inner loop not changing the value of outer a 

print ("Python isn't easy language: ", end ="") 

def outer(): 

    a = 'Yes'

    def inner(): 

        a = 'No'

    inner() 

    print (a) 

outer()

Output

Python is easy language: No

Python isn't easy language: Yes


Conclusion

Above we have discussed about the variable scope in python programming language. Scope is a region from where we can access the variable. In simple words, that part of a code where the variable is visible in python and from where we can use it when it is required is known as scope of variable. There are four types of variable scope in python i.e., Built-in scope, Global scope, local scope and enclosing scope.