Python. The scopes of names in Python. Local names. Visibility rules for names. LEGB rule. The global keyword. Overriding names in functions

The scopes of names in Python. Local and global names. Visibility rules for names. LEGB rule. The global keyword. Overriding names in functions


Search other websites:

1. The meaning of names in Python

In the Python programming language, everything about the scope of name is determined based on the assignment operation. As soon as a name is encountered in the program, the interpreter processes it accordingly. This can be to create, change, or search a name in a namespace (the area where names are placed). In this case, a namespace is a place in the code where a value has been assigned to a name. This namespace defines the scope of the given name.


2. In what scopes can you assign values to variable names?

According to Python philosophy, every variable has a name and is an object. The name of a variable appears in the program after assigning a value to it. Once assigned, any name has its own scope (namespace). According to the scope of a variable, this variable can be classified in one of three possible cases:

  • a local variable is a variable that is assigned a value inside a def statement (inside a function) or lambda statement;
  • a non-local variable is a variable that is assigned a value within the bounds of the enclosing def statement;
  • global variable is a variable that is assigned a value outside of all def statements (outside of all functions) or in the middle of a def statement with the global keyword.


3. Local names. Local name visibility rules

The Python language allows you to declare functions using the def statement. Within this instruction, you can define your own names. Thus, the concept of a local name arises. A local name is a name that is created (defined) inside a function. Functions define nested namespaces that restrict access to names defined within those functions.

The namespace of a local name is determined by the namespace of the function in which this name is defined. For local names, the following visibility rules can be formulated:

  • a name that is defined inside a function (inside a def statement) is available only in the program code of this function from the moment it was first used;
  • names that are defined inside a function do not conflict with names that are declared (used) outside the function. Thus, the same names can be used inside a function (def statement) and outside of it;
  • outside of the function, there is no access to local names declared inside the function.


# Local names

# Defining names outside a function - global variables
a = 'Hello world!' # Name a, which is a string
b = [ 2, 3, 8.55, 'Textbook' ] # Name b - list

# Defining names within a function
def Fn():
    a = 2.777 # name a - floating point number
    b = { 1:'One', 2:'Two' } # name b - dictionary
    c = [ 0.1, 0.01, 0.001 ] # name c - list
    print('Fn.a = ', a)
    print('Fn.b = ', b)
    print('Fn.c = ', c)

# Print names that are declared outside the function
print('a = ', a)
print('b = ', b)

# Invoke function

There are 5 names in the above code. Three names are declared inside the function. These names do not conflict with names that are declared outside the function, even if they are the same. So, the name a outside the function is a string. Such a name inside the function is a floating point number. This means that these names are located in different namespaces.

The result of the program

a = 'Hello world!'
b = [2, 3, 8.55, 'Textbook']
Fn.a = 2.777
Fn.b = {1: 'One', 2: 'Two'}
Fn.c = [0.1, 0.01, 0.001]


4. What scopes are there in Python?

There are four scopes in Python:

  • built-in scope. This scope includes known names in the unit of built-in names. Examples of such names: print, open, set, range, slice and others;
  • global scope within the module;
  • local scope of enclosing functions;
  • local scope inside functions. Such names are declared inside a def or lambda statement.

Figure 1 shows access to names in different scopes.

Python. Name scopes

Figure 1. Name scopes in Python


5. How are scoped names searched? How the LEGB rule works

If a name is declared in a function (local scope), then the interpreter looks for this name in the following order:

  • in the local scope inside the function (Local);
  • in local scopes of enclosing functions (Enclosing);
  • in the global scope (Global);
  • in the built-in scope (Built-in).

This order follows the so-called LEGB rule: Local – Enclosing – Global – Built-in.

If there is a reference to a name in the global scope, then the search occurs in this scope and also in the built-in scope (see Figure 1).


6. How do I determine built-in scope names? Example

The built-in scope names are placed in the standard library module builtins. To get a list of names, you need to import this module. The list of names is obtained using the dir function.

# Defining the names of built-in scope

# Import the builtins module
import builtins

# Get a list of names
names = dir(builtins)

# Print names in loop
for t in names:

After starting the program for execution, a list of built-in names will be displayed.

If the program includes the builtins module as follows

import builtins

then using the name builtins. you can refer to built-in names. For example

# Create empty set
A = builtins.set()

# Create empty list
L = builtins.list()


7. What are the relationships between local and global scopes?

The following relationships exist between local and global scopes:

  • a module is a global scope (namespace). This is the scope where top-level global variables are created in the module file. Outside of a module, global variables are attributes of the module object;
  • the global scope is defined in a single file. Global names within this file always refer to this module;
  • calling any function creates a new local scope. This scope has boundaries that are defined by a def or lambda statement;
  • if recursion is implemented in the program code (the function cyclically calls itself many times), then each function call creates a new local scope (namespace);
  • if the assignment operator = is used inside the function, then this operation creates local names by default. If you need to declare a global name inside a function, then the global keyword must be specified before this name. If a lower-level def statement (in the case of nested functions) needs to be assigned the value of a name that is declared in a top-level def statement, then that name can be declared with the nonlocal keyword.


8. How do you override built-in and global names inside a function? Example

According to the LEGB rule, built-in and global names can be overridden within a function.

Example. The following code overrides the global name x and the built-in name list.

# Overriding built-in and global names within functions

# 1. Overriding the built-in name list in the Fn1() function
# 1.1.Define Fn1() function
def Fn1():
    list = 'This is a text'

# 1.2. Invoke function Fn1()

# 2. Overriding global name x inside function
# 2.1. Define the global name
x = 30

# 2.2. Define a function Fn2 () that uses its own local name x
def Fn2():
    # Define local name
    x = 'Local x'

    # Print local name
    print(x) # Local x


# Invoke function Fn2()

# Print global name
print(x) # 30

After running the program produces the following result

This is a text
Local x

As you can see from the result, the built-in and local name are overridden by local names within the function.


9. Declaring a global name inside a function. The global keyword. Example

A global name within a function can be declared using the global keyword. In general, such a declaration looks like this:

def FuncName(parameters):
    global var
    var = value


  • FuncName – the name of function;
  • var – a name that is declared global inside the FuncName function;
  • value – assigning some value to a global name.


# Global variables. The keyword global

x = 25 # global name

def Fn():
    # declaring global name t inside the function
    global t
    t = 30 # assign the value to global name t

    # print global name x inside the function


# Using global name t declared in the function Fn()
z = x+t # z = 25+30 = 55


Related topics