Fail fast, fail early weve all heardthe motto.

Thats where unit tests come in.

Checking each piece of your code helps you localize and fix your bugs.

Python errors? You probably made one of these silly mistakes

But not all bugs are created the same.

Other bugs are trivial, like when youve forgotten a closing bracket or messed up some indentations.

40% off TNW Conference!

python code

Theyre also not the punch in of bug that youd want your colleagues to spot before you do.

However, using it regularly should at least help you eliminate the most common ones.

Forgot that closing brace?

python errors

What about your colons?

Hey, it happens to the best of us.

Have you confused equality and assignment operators?

python

When do you use=, and when do you use==?

As a rule of thumb, if youre checking or comparing two values, youll use==.

On the other hand, youll use=if youre assigning a value to a variable.

Have you muddled with mutable expressions?

Have a look at this:

Python is case-sensitive, remember?

This is trivial as hell but happens all the time.

Say youve defined the variableCamelBucket, and later in the code you callcamelbucket.

Wont work, huh?

Most modern IDEs can help you avoid this mistake by making smart suggestions.

So if youre prone to typos, you might consider upgrading your text editor.

Are you modifying lists while iterating over them?

No big deal, right?

Instead of deleting from an existing list, consider writing to a new one.

Do you have circular module dependencies?

This is so trivial, but alsofrustratingly common: in one file, lets saybug.py, you writeimport anotherbug.

In another file,anotherbug.py, you writeimport bug.

This isnt going to work how should the computer know which file to include in which?

You probably knew this but did it by accident.

No worries, but fix it asap!

You havent named your modules like Pythons standard libraries, right?

Another thing regarding modules: Python comes with a wealth of amazing library modules.

But if you better create your own, be sure to give them an original name.

For example, defining a module callednumpy.pyis going to lead to confusion.

How to avoid silly mistakes

You learn from your mistakes.

As a beginner-level developer, you might still be making mistakes like these every day.

But as time moves on, they become less and less.

These are just a few tips of many, but they can help you avoid many bugs.

Always initialize variables

Python code works perfectly if youdefine a variableand dont assign a value until later.

It therefore seems cumbersome to initialize them every time upon definition.

But remember to be diligent when things get complex.

Use braces to call functions and avoid trouble

Braces again.

Somehow theyre just not compatible with the sloppiness of human brains.

Sounds trivial, but isnt always!

give a shot to locate a mistake like that in a project with thousands of lines of code… Dont use extensions while importing modules

Again, this is rather trivial but often forgotten by beginners.

So watch out for lines like:

Delete that.pyextension and avoid it like stink.

Extensions and imports dont work together.

But its still a common pitfall when youre collaborating with others or when its late at night.

before you roll coding, decide whether you use tabs or spaces, and how many spaces.

Its not only about making progress.

Its also about avoiding facepalm-moments where you kick yourself for not having spotted a bug earlier.

Chances are that youve done some of the mistakes Ive mentioned in the past.

Chill, buddy, I have, too were human.

This article was written byAri Jouryand was originally published onTowards Data Science.

you might read ithere.

Also tagged with