• author: Python and Pandas with Reuven Lerner

Understanding Three Different Types of Errors When Running a Python Function

As a Python developer, it is crucial to be familiar with the different types of errors and their causes when running a function. In this article, we will explore how one buggy function can produce three distinct error messages, and why it is important to understand each of them.

Defining the Function

To demonstrate these errors, we will first define a simple function with a non-existent variable:

def my_func():
    blah blah blah

Surprisingly enough, no error is raised at the time of defining the function. We can even check the type of my_func using type(my_func) without any issues. So how is this possible?

The answer lies in Python's runtime name-checking. Only when the function is executed does Python search for the name blah blah blah. At that point, if the name is not found, an error occurs.

Error #1: Name Error

To produce the first type of error, we call the function using my_func():

NameError: name 'blah' is not defined

This error occurs when Python fails to find a variable or function name. In this example, Python attempts to find a local variable named blah while looking for a local variable. Since the name blah doesn't exist, Python proceeds to look for it globally and then among built-in names. Since it cannot be found anywhere, a NameError is raised.

Error #2: Type Error

To produce the second type of error, we must call the function using arguments that don't match the function definition. For instance, we might try to call my_func() after defining it with arguments:

TypeError: my_func() takes 0 positional arguments but 3 were given

In this scenario, Python is unable to run the function due to a type mismatch between the function definition and the arguments we have provided. Python recognizes that the function is defined with zero parameters, and yet we have provided three positional arguments (10, 20, 30). Python therefore cannot map the arguments to any parameters in the function signature and returns a TypeError.

Error #3: Syntax Error

The final error occurs when syntax rules are not met. For example, we might format the function call improperly by first providing a keyword argument (a=10) before all the positional arguments (20, 30):

SyntaxError: positional argument follows keyword argument

This error can be tricky to notice, as the function doesn't even run. Python fails to parse the function because we violate syntax rules regarding keyword argument order. As a rule, keyword arguments must come after all the positional arguments.

Importance and Takeaways

By understanding these different errors and their causes, developers can diagnose problems and write better code with fewer bugs. Python's robust error messaging system provides context into what's going on behind the scenes and helps us to identify issues quickly and precisely.

When you encounter errors, it is essential to pay attention to them and understand what they mean so you can troubleshoot effectively. This practice will help you write better code and be more productive when working on Python projects.

We hope this article was useful to you. If you have any questions or comments, feel free to leave them below. Stay tuned for more Python, pandas, and data science content!

Previous Post

Exploring the Python Standard Library Series: Methods to Change Lists

Next Post

A Guide to Discussions in the Net Logo Modeling Commons

About The auther

New Posts

Popular Post