Python local scopes

Python has four scopes and among them, we will discuss the Python local scopes in this post. The local scope refers to the innermost region in a program. It also has the least influence in a Python program. Variables or objects having local scope can be accessed only inside the local region. They are restrained from being available outside the local region.

Link :Python scopes types

The most common example of local scope variables are the variable inside a function. Such variables can be accessed only inside the function. Trying to access them outside the function is an error.

>>> def Integer():
def Integer():
	local_var=901 #local variable
	print( local_var )

>>> Integer()
>>> print( local_var ) #trying to acces the local variable
Traceback (most recent call last):
  File "<pyshell#47>", line 1, in <module>
    print( local_var ) #trying to acces the local variable
NameError: name 'local_var' is not defined

Since local_var is a local variable accessing it outside the function is an error. The error message above ‘name ‘local_var’ is not defined‘ means the local_var is not known outside the func() region.

Link : Python enclosing scopes

NOTE!! The explanation below is related to ‘class’ do not proceed further if you do not what is a class. Once you have learned what class is, you can always come back and read the discussion provided below. However, read the ‘Side note‘ provided at the bottom of this post.

Local scope variable inside class

A local scope variables can also exist inside a class. The local scope can be just for the class or it can be for the function inside the class.

If the local variable has an influence only inside the fucntion then the class cannot access it, although the function can still the local variable of the class.

class NewClass:
	m=901 #local variable
	def func():
		func_locaVar=169 #local variable of func()
		print( func_locaVar )
		print( '\n',m )

>>> NewClass.m
>>> #Trying to access the 'm' local variable
>>> print( m )
Traceback (most recent call last):
  File "<pyshell#58>", line 1, in <module>
    print( m )
NameError: name 'm' is not defined
>>> #Trying to access the func() local variable 'func_locaVar'
>>> NewClass.func_locaVar
Traceback (most recent call last):
  File "<pyshell#59>", line 1, in <module>
AttributeError: type object 'NewClass' has no attribute 'func_locaVar'

We can access the local variable of ‘m’ using the class name but we cannot access the ‘m’ variable directly because it only belongs to the class.

Using the class name we still cannot access the func_locaVar because it has influence only inside the func() region. However, we can access ‘m’ inside func() because ‘m’ has scope over the func() function.

Side note

Points to note:
i)Local scope variable has the most influence among all the scopes in local scope.
ii)But note local scope variables have more power over the other scopes variable in the local region. Meaning, local variable value will be always prefered over values in the local region.

>>> def func():

>>> m=9100 #global scope 
>>> func()

The local scope variable value is considered inside func() not the global scope variable.

iii) The order followed by Python while looking for names in a scope or namespace is : local scope> enclosing scope > global scope > built-in scope. This order is also known as ‘LEGB‘.