Error tracebacks are your friends

Hey there, 👋

How is your Python going?

In this Mathspp Insider đŸđŸš€ email we’ll talk about errors and tracebacks.

This email at a glance:

  1. Tracebacks are your friends. Don’t be afraid of them.

  2. Always remember to read the error type, the error description, and the call stack.

  3. Use `pip install error-links; error-links install` if you want Google/documentation links next to errors when they happen.

  4. Signups for the 2nd edition of the Python problem-solving bootcamp are open.

  5. Get 50% off with the code p638rk5 until Friday.

Let’s dive in!

Learn problem-solving skills

Everyone knows practice makes perfect.

If you want to become better at problem-solving with Python, I have 42 problems for you to practice with.

This December, I’m running an asynchronous cohort where we’ll be solving 42 problems over the course of 21 days (2 problems/day).

You’ll learn more about the way Python works, you’ll learn about tools from the standard library, and you’ll improve your problem-solving skills.

How does the bootcamp work?

Simple.

Each day you get one problem.

You solve it with Python.

You get a follow-up problem.

You solve it with Python.

Whenever you have questions or difficulties, you ask in the private Discord channel.

After you’re done, you submit your code.

I collect everyone’s code and write an analysis of the problem discussing different approaches.

We do this 21 times.

By the time we’re done, you’ll be tired but you will:

  • know more useful tools and modules from the standard library;

  • have 21 notebooks analysing the 42 problems in detail;

  • be able to tackle problems from new perspectives; and

  • know more general-purpose algorithms and coding techniques.

If you want to improve your Python problem-solving skills, just click the button below.

If you have any questions, feel free to reply to this email!

P.S. Because you’re an insider, you can use the code p638rk5 to sign-up for 50% of the price until Friday.

Error tracebacks are your friends

Error tracebacks are your friends when you are writing Python code.

If you have a bug and Python breaks and complains…

That’s excellent!

It’s much worse when you have a bug and no one knows: neither you nor Python.

Those bugs are much worse.

If you made an error and Python shows a traceback, that’s fine.

The first thing you must do is to read the error traceback.

Does it sound obvious?

I hope it does!

I see lots of people writing code, then their code has an error, and they don’t even read the error traceback.

They just jump straight to the code looking for the source of the error…

Spoiler alert: the source of the error is written in the traceback!

Here is an example of a traceback I got when I ran a script:

❯ python myapp.py
Traceback (most recent call last):
  File "myapp.py", line 134, in <module>
    CSSErrorApp().run()
  File "./src/textual/app.py", line 375, in __init__
    1 / 0
ZeroDivisionError: division by zero

You should always read the bottom three lines of a traceback, from the bottom up:

  1. ZeroDivisionError: division by zero – the bottom line of a traceback tells you what type of error you had, it describes the error, and sometimes it even suggests a fix!

  2. 1 / 0 – the line of code that actually broke, which in this case is a division by 0 that I inserted in my code on purpose.

  3. The file, line number, and function where the error happened – this is useful information that points you to where the error happened.

The more error tracebacks you read, the better you’ll become at reading them.

With time, you’ll start to recognise error types and you’ll be able to predict exactly how you screwed up without even needing to read your code.

(Of course you won’t be able to do this always, but it’ll be much easier to catch “silly mistakes”.)

Helpful “google me” and Python docs links

Because I often see beginners struggle with reading error tracebacks, I created a Python package that does this:

Helpful error links next to Python exceptions.

When there is an exception in your Python code, my package error-links will insert a link for you to automatically Google that error + description and it will also link directly to the section of the Python documentation that explains that error type.

You can find my package in the error-links GitHub repository.

To install the package, run `python -m pip install error-links`.

Then, if you want error-links to always be active, run the command `error-links install`.

(You may need to restart your terminal between installing with pip and running the command `error-links install`.)

Tracebacks don’t point at logical mistakes

One thing to always bear in mind is that when Python prints a traceback, it’s the error traceback of the issue that Python found.

An error traceback might get printed in a place that is different from the place where the actual error happened.

This is particularly common if the mistake you made was a logical mistake somewhere, which eventually led to Python tripping up in a completely different place.

ICYMI

In Case You Missed It, over the past few days I published 8 articles of a series called “Building a Python Compiler and Interpreter”, which is a series of blog articles where we’re essentially our own version of Python by implementing a tokenizer, a parser, a compiler, and an interpreter, from scratch.

If you want to see what it takes to implement a programming language from scratch, give it a read here: https://mathspp.com/blog/tag:bpci.

🐍🚀 How was this email?

Login or Subscribe to participate in polls.