DEV Community

Cover image for Checking your python code?
Code Inspector

Checking your python code?

Enthusiastic Learning! Teaching/Learning about Coding and Machine Learning ๐Ÿค– Travel - Hacks - Tech
ใƒป2 min read

Hello world! Let's talk about python ๐Ÿ
Whether you are a beginner or an advanced python developer, it is a good habit to check your code frequently (while writing, deploying or refactoring). You might wonder, where to start or how to start, here some ideas to check your python code ๐Ÿ๐Ÿ’ป

But first, recall about good code! ๐Ÿค“ High-quality code should do what's meant to do (functionality), should not have problems or other errors, and should be easy to read and maintain.

โœ… your python code should be easy to read for others (First checkpoint!)

There are different tools, that can help you check your python code. I'll mention a few of them, that I've been trying:

  • Pylint supporting with Coding standards, Error detection, Refactoring help, among others.

  • Pytest helps you write better programs

Although, both can help you write good coding, also check these general remarks that will guide you in your regular checkings:

  1. Check spaces โœ…
    Coding on python? spaces are super important! Remember to use whitespaces after a colon, semicolon, or comma, as well as when using operators (it is a good practice!) to have a whitespace ' ' before and after an operator (=, <, >, etc).

  2. Booleans โœ…
    Avoid repetitions... if you are writing this:

var flag = TRUE
# something something

if flag == TRUE
Enter fullscreen mode Exit fullscreen mode

Consider in better writing this:

var flag = TRUE
# something something

if flag
Enter fullscreen mode Exit fullscreen mode
  1. Handling errors โœ… If writing this...
    #something something
Enter fullscreen mode Exit fullscreen mode

Consider writing this...

    #something something
except TypeError:
Enter fullscreen mode Exit fullscreen mode

We can go on and on, talking about recommendations to have a high-quality python code! I recommend checking out pytest and/or pylint and see how much better you are getting at coding in python ๐Ÿค“ and also if you would like to automate and learn more about common errors and best practices, check Code Inspector helping to build code better, faster.

Alt Text

Discussion (1)

gsandec profile image
Gustavo Silva

Great article Code Inspector!
Here's an article about python static analysis tools we hope it adds value to your readers.
Keep on with great articles!