- Mathspp Insider đđ
- Posts
- Error tracebacks are your friends
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:
Tracebacks are your friends. Donât be afraid of them.
Always remember to read the error type, the error description, and the call stack.
Use `
pip install error-links; error-links install
` if you want Google/documentation links next to errors when they happen.Signups for the 2nd edition of the Python problem-solving bootcamp are open.
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:
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!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.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? |