Python NameError – Tutorial with Examples

Python NameError - Tutorial with Examples

Python is one of the most popular programming languages in the world because of its powerful syntax and flexibility. However, even experienced Python developers can sometimes face certain errors, one of which is the NameError.

What is a NameError in Python?

As the name suggests, a NameError is an exception that occurs when Python fails to recognize a name or identifier that we have used in our code. The error occurs when we use a variable or a function that has not been defined yet or is not in the current scope.

A NameError is raised with the message, “name 'x' is not defined“, where ‘x‘ is the name or identifier causing the error. The error message can also vary depending on the context in which the error occurred.

Let’s look at some examples of NameErrors in Python to get a better understanding of the error:

Example 1: NameError: name ‘spam’ is not defined

In this example, we are trying to print the value of a variable called “spam“, but the variable has not been defined. Therefore, when we try to execute the code, Python raises a NameError with the message, “name 'spam' is not defined“.

# example 1
print(spam)

Output:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'spam' is not defined

To fix the error, we need to define the variable “spam” before we try to use it:

# example 1 - fixed
spam = 42
print(spam)

Output:

42

Example 2: NameError: name ‘max_size’ is not defined

In this example, we are trying to print the maximum size of a list using the max_size() function. However, we get a NameError because the function is not defined in our program. The error message says “name 'max_size' is not defined“.

# example 2
my_list = [5, 10, 2, 7, 1]

print(max_size(my_list))

Output:

Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
NameError: name 'max_size' is not defined

To fix this error, we can use the max() function instead of max_size():

# example 2 - fixed
my_list = [5, 10, 2, 7, 1]

print(max(my_list))

Output:

10

Example 3: NameError: name ‘x’ is not defined

In this example, we are trying to print the value of a variable called “x” inside a function, but the variable has not been defined inside the function, therefore we get a NameError with the message, “name 'x' is not defined“.

# example 3
def my_function():
    print(x)

my_function()

Output:

Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
  File "<stdin>", line 2, in my_function
NameError: name 'x' is not defined

To fix the error, we need to define the variable “x” inside the function before we try to use it:

# example 3 - fixed
def my_function():
    x = 42
    print(x)

my_function()

Output:

42

Conclusion

Python NameErrors are common errors that occur when Python fails to recognize a name or identifier used in the code. The errors can be fixed by defining the variable, function, or module before we try to use it. A clear understanding of the error message and the context in which it is raised can help us debug and fix the error quickly.

Leave a Reply

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