Python built-in scopes or Python outermost scopes

The Python built-in scopes or Python outermost scopes contain the built-in names. As the name states, it is the outermost scope among all the scopes. Any names are searched for in this scope at the last.

Link : Python scopes types

Before we see what is built-in scopes exactly let us see what built-in names mean.

Built-in names

When we say built-in names we are referring to those names which carry certain meanings. The built-in names are the names given to an object or a function or a certain program’s condition. Some instances of built-in names are built-in functions names like abs() , all(), any(), tuple(), zip(),etc. and the exception names like ZeroDivisionError, NameError, TypeError,etc.

Note built-in names are not similar to reserve keywords, there exist some differences between them, their differences are explained in another post.

Link :Python difference between keywords and built-in names

Built-in scope example

Suppose you want to call a function name abs(), so you write it in your program. In executing the program, Python will automatically call the built-in function abs(). Consider the code below.

Link : Python abs() built-in function

>>> abs( -90 )

So why built-in abs() is called? What actually happens is, first of all Python searches for any function with the name abs() in the local scope followed by the enclosed scope and then the global scopes. Since it could not find any function with the name abs(), Python now turns towards the built-in scopes. It searches for any similar name in the built-in scopes and ultimately it found one. Hence, it calls the built-in ‘abs’ function.

Suppose, we have defined a function with the name ‘abs’ in any of the scopes other than the built-in scopes, then that function would be preferred over the function present in the built-in scopes. Well the reason is, the local scopes or the enclosed scopes or the global scopes has more power than the built-in scopes.

>>> def abs( var ):
	print( 'var=',var )

>>> abs( 9000 )
var= 9000

We have defined a function name ‘abs’ of our own and hence it is called, not the built-in scope ones.

***Side note

i)Built-in scopes or outermost scopes is the last place where Python searches for any names.

Always remember, 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 *