Common Python pitfalls

Here are mistakes I have observed which lead to broken code.

  • Python is not Java, C, or C++ and needs to be treated as its own language.

Treating it with the same syntax is a sure fire way to make broken code. Java and Python are both object-oriented languages. French and Italian are both Romance languages. You can’t walk into a pub in Milan speaking French or a pub in Bordeaux speaking Italian and expect people to understand you. English with German grammar means people think of you weird will. For the same reason, you cannot try to render Java code in your Python renderer and expect it to work. It has to be fully one way or the other. Take the time to learn the target language, use a linter, and you will  write better code.

  • Value equality vs reference equality. == and is are not the same! What are the differences? What happens if I use the wrong one?

This goes back to C. In C there are pointers, which points to places in memory. This is a reference equality. In python this is represented by the function is. The other value equality is asking if two objects have the same value. In complicated code, this can become very important, particularly when dealing with users input.

For more details on how this can break code quickly, please look through this helpful notebook I wrote.

  • Close your statements, these go bump in the night, hard to trace, waste time, break your code

Modules are wonderful. Modules make it so that instead of writing out every piece of code every time, you can import a module to do what you want reliably and quickly so you can get to the money making code.

Modules also have dependencies, and will often use the same dependency. For example, Pandas uses csv as a dependency. If you try to open a file with the csv module, and then use Pandas on the module, you run into a problem because its already open. You can prevent this problem by not keeping functions open.

If you want to write to a file, don’t do this:

import pandas as pd
file = csv.reader('random.csv')
file.write('pizza,anchovies,tomatoes')
pd.read_csv('random.csv')

Since Pandas imports CSV this is going to fail, but it won’t give you a clear error message. This is dangerous. Always close your statements. Using established modules prevents this problem because they are used by millions of people every day.

  • Try to use the exceptions correctly, how can this break your code? Those who hide errors pay the price

Exceptions can be very useful if you need to fail in a particular case. They also are the worst bugs you will ever find in your life, especially if they hide debug to fix your code. If you use code in a way which explicitly states which exceptions you use (when necessary) and use if statements when possible, then errors which occur in dependencies won’t get buried and your debugging experience will be far more pleasant, less time consuming, and fruitful.

This is a new idea to Java and C developers, but it really does prevent many hours of tedious and unsatisfying work.

  • Default to 0 when data doesn’t exist, a great way to screw up your data and waste hundreds of hours of expensive developer time

They’re different values. Just don’t do it. It makes it impossible to filter your data accurately. It makes dirty data. It wastes time. You will regret it.

  • What happens if I don’t use the Oxford comma?

If you ever want to split a string into a list, you will very quickly learn to love the Oxford comma.

  • Reassign built-ins

Reassigning built-ins can mean that if you try to use the built in, either explicitly, or in a dependency, that it will go to what you assigned it. Just. Don’t.

  • Why not to use System Python, and how to have stability by using virtual environments

If you plan on using the computer for an extended period of time and the system updates, it will change the python dependencies on your computer. This can make it so version incompatibilities arise. Using virtual environments containerizes your code so your operating system won’t change it and gives you full control. It only takes one time to have all of  your dependencies screwed up to learn why you should use virtual environments and containers.

 

Hopefully these 10 pitfalls will help you write better code.

Leave a comment

Discover more from Stidmatt

Subscribe now to keep reading and get access to the full archive.

Continue reading