8 signs you’re doing Python right

If you’re following these eight great Python programming practices, then you’re doing Python right

8 signs you’re doing Python right
ComicSans / Getty

It’s often easy to tell when you’re doing development wrong. Bugs that pop out of nowhere, code that runs like molasses, and unwieldly project structures are just three of the tell-tale signs. But sometimes it’s not so easy to tell when you’re doing it right. Good programming habits are like the best special effects in the movies: You never notice they’re there, because they don’t announce themselves.

So what are some of the signs of good programming practice in Python? If you’re new to the language, and you could use some idea of what good Python development behavior looks like, or you’d just like to know if some of the habits you’ve picked up are worth keeping, we’ve collected a set of guideposts for you. If you’re following the eight best practices outlined below, then you’re doing Python right. 

You’re using virtual environments and project management tools

Even small, one-off Python projects benefit from a virtual environment and a proper project structure. Tools like Poetry make the setup process all but automatic. Even lowly venv makes it relatively straightforward to populate your project with a virtual environment and reap its benefits.

Version control is another vital project management tool. These days that typically means Git. And yes, you should take advantage of Git even for simple one-file projects. You never know when you’ll need to roll back to an earlier version of something. Most IDEs that support Python also have built-in support for venv and Git, so it’s easly to make use of them.

You’re using Python built-ins and not reinventing the wheel

The savvy Python programmer knows to look to Python’s built-in functions and standard library to handle the vast majority of common use cases. Most any routine task you will need to accomplish — file manipulations, regular expressions, working with a web browser, etc. — are already handled in Python somewhere.

A common question less experienced Python developers ask is, how do I know whether something in the standard library covers my use case, without having to memorize the contents? The short answer is to bookmark the standard library index and built-in functions overview pages and skim them whenever you find yourself wrestling with an “everyday programming” kind of problem. Constant exposure to tools available inside Python is a powerful teacher.

You’re using the right modules for the job

As with built-ins and the standard library, making use of Python’s enormous collection of third-party modules is a labor-saver. Fire a search term for your problem through PyPI’s web front end, and you will almost always find modules that provide an out-of-the-box solution. Again, why reinvent the (Python) wheel when you can just pip install it? Plus, many Python modules feature C-accelerated performance, meaning you get faster development speed and faster program execution when using others’ modules.

One question that comes up often: If multiple modules are available that handle my use case, which one should I use? The easy answer is to start with whichever project seems to have the most usage or momentum (as indicated by GitHub stars or forks, for example), because there’s a good chance your use case is the same as a lot of other folks.

You’re not going crazy with OOP

Object-oriented programming (OOP) bundles data structures with the methods used to manipulate them, making it easier to write high-level code. For a high-level language like Python, OOP is a great fit. But not all Python code needs to be object-oriented. Python developers in the know understand that 20 lines (or even 200 lines) of get-it-done-and-get-on-with-it code can be a valuable timesaver versus an object-oriented alternative, as long as the job doesn’t need the OOP approach. On the whole, large and complex projects do benefit from object orientation, but unless the project demands OOP, it’s okay to relax the rules.

You’re testing your code

Speaking of something done with bigger projects, wise Pythonistas know the value of creating test suites. As tedious and tiresome as writing tests can be, they’re invaluable when a project grows past a certain level of complexity. A sign of even more advanced mastery is building tests in parallel with each new feature, not after the fact. Really, the sooner you get over the test-writing hump, the sooner you will begin to improve your code. Start testing right with the first feature implemented and you’ll be glad you did.

You’re not using Python 2

This was true last year and it’s doubly true now. Python 2 had a long, good run, but it’s now entirely unsupported by the official Python development team. Wise Pythonistas have already migrated their projects to Python 3 or they are doing so now.

You’re not using the latest version of Python, either

Python 3.8 may be the latest and “greatest” version of Python, but that doesn’t mean you are obliged to use it. In fact, the smart software developer understands that using the most recent version of anything is a roll of the dice. Python is no different. The savvy Python programmer sticks with the revision before the most recent revision to ensure the broadest compatibility with third-party modules. After all, the ability to draw on all those useful modules is one of the main reasons to use Python in the first place.

You’re using other programming languages, too

Python has been described as the “second best programming language” for just about everything—and sometimes the gap between best and second best is wide indeed. Wise Python developers know that Python is just one tool among many, and that you should always use the right tool for the job at hand.

For applications or operations requiring maximum runtime speed, you probably don’t want to roll something in pure Python; it’s C or Rust or even Go you’ll be reaching for. But not all performance-intensive programming means abandoning Python entirely. Maybe it just means using a C-powered library like Numpy or Pandas, or wrapping C or C-compatible code in a Python shell, or using Cython to transform Python into C.

Read more about Python

Copyright © 2020 IDG Communications, Inc.