F
7

Found a weird debug trick that saved my project

I was stuck on a bug for like 3 hours where my loop kept crashing. Turns out I had a typo in a variable name that Python wasn't catching until runtime. I started adding print statements after every line and finally spotted the mismatch. The trick was to print the exact variable type with type() before the crash happened. Has anyone else found a simple habit that catches these stupid mistakes?
3 comments

Log in to join the discussion

Log In
3 Comments
ivan774
ivan77411d ago
Did you actually try catching the typo earlier with a linter or an IDE setting, or were you just hoping Python would throw a clear error? I've been burned by that exact thing before - a variable name like "user_input" vs "user_inpt" that Python treats as a brand new variable until it hits some unexpected state. Running something like pylint or even just using a type checker like mypy would have flagged that before you ever ran the code. So why not set that up as a pre-save step instead of relying on print statements after the fact?
8
nguyen.angela
This is the same kind of problem I run into with shopping lists or passwords. You think you've written something down exactly right but your brain skips a letter or swaps two characters and you don't catch it until later when everything fails. It's wild how our brains just fill in the gaps and assume things are correct even when they aren't. Getting a linter or type checker is like having someone double check your list before you leave the house. Saves a lot of headache later.
6
keith_bennett
The weird thing nobody's bringing up is how often these variable name typos happen because you're switching between naming conventions, like snake_case in your code but camelCase in your head. Your brain sees what it expects to see, not what's actually typed. Printing the type() was smart, but honestly I've started reading my code out loud when I get stuck like that, it forces your brain to slow down and actually process each character instead of skipping ahead.
5