Before diving into the classes we must know about what is local and what is global variables. How they are accessed in python programming language and what are the limitation of these variable.
Local variable is a variable that we normally assign. Its value is only valid to that scope and can’t be accessed outside the scope. Following example will clear the concept of local variables.
We assign the variable out outside the function and before defining it and in inside the function scope. Function can access both the variable but when we try to print the ‘in’ variable outside the scope it generates an error. The output of the above written program is following:
As we can see that both print function inside the function body is executed and ‘out’ variable is executed in sequential print but ‘in’ variable is not printed. This is because python do not access the function variable.
Global variable is a variable that can be accessed from every. There is no boundary of scope that affect the global variable. The above example can be converted into such example so that we can access all the variables even outside the block.
Non-local variables are those variables which make changes the module level. The type of changes is permanent at module level. It cannot be declared outside the module. Unlike global variable which can be defined anywhere nonlocal can only be defined within a function block. Following example will show collectively how different are local, nonlocal and global variable from each other.
As we can see in the output section the nonlocal change function binding and even after global changing there is no changing within a function. Global is than accessed outside the block. In this way we can control the data integrity. We can control who can see the data and who cannot.