Python scopes

When we say Python scopes or just scopes in a Python, we are referring to the textual region in a Python region where a namespace is directly accessible. In other words, Python scopes refer to the area where the object is accessible using the name of the object (if you are confused as to what scopes mean do not worry it will become clearer later on).

There are four types of scopes in Python:
 
i)Local scopes.
 
ii)Enclosing scopes.
 
iii)Global scopes.
 
iv)Built-in scopes.
 

A brief description of each of these scopes is given below.


Local scopes

The local scopes refer to the innermost region and it contains local names. The variables or objects in local scopes can only be accessed inside the local region. It cannot be accessed from anywhere except the local region.

Consider the code below. The function func() defines a local scopes variable name ‘m’.

>>> def func():
	m=901
	print( 'm=',m )

>>> func()
m= 901
>>> print(m) #Error
Traceback (most recent call last):
  File "<pyshell#32>", line 1, in <module>
    print(m)
NameError: name 'm' is not defined

Do you notice, the ‘m’ variable cannot be accessed from outside the function. Trying to access it outside will give an error message. The variable can be accessed only within the func() function because it has a local scope i.e. the func() function region.

Note** local scopes variables or objects can be accessed only within the local scopes region.

More about local scopes here : Python local scopes



Enclosing scopes

When we say enclosing scopes it refers to the scopes which are bigger than the local scope but lesser than the global scopes. The enclosing scopes have an influence over some of the local scopes but it does not have an influence over the entire region like the global scopes.

An instance of enclosing scope is a function enclosing another function. More about this is discussed in another post.

]
More about enclosing scopes here: Python enclosing scopes


Global scopes

The global scope has an influence over the entire region. It contains the global names. The global scope variables or objects can be accessed anywhere inside the programs.

Consider the code below. The variable ‘m’ is a local scope and the variable ‘g’ is a global scope.

 
>>> g=100 #glocal variable
>>> def func():
	m=12345
	print('m=',m)
	print('g=',g)

	
>>> func()
m= 12345
g= 100
>>> print( g )
100
>>> print( m )
Traceback (most recent call last):
  File "<pyshell#41>", line 1, in <module>
    print( m )
NameError: name 'm' is not defined

Since ‘g’ is a variable having the global scope it can be accessed even inside the function func(). The ‘m’ variable being a local variable cannot be accessed outside.


Built-in scopes

The built-in scopes refer to the scopes containing the built-in names. The built-in names refer to the Python functions names or keywords which is part of the Python language. Those functions and keywords can be used anywhere within the Python programs. The region within which these functions or keywords can be used are known as built-in scopes.


Side note

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




Leave a Reply

Your email address will not be published. Required fields are marked *