The world’s leading publication for data science, AI, and ML professionals.

How To Write Python Code Like A Senior Developer

Improve your coding style using Pylint.

Photo by Joshua Aragon on Unsplash
Photo by Joshua Aragon on Unsplash

Many of us had embarked upon our self-taught developer endeavour. Reading on Medium, using MOOCs (Massive Open Online Course) or watching videos. And that’s ok. Coursera, Udemy, Codecademy… all of them are great ways of learning how to code. But there’s always a moment to take action. And when you start working for a company you need to improve your fashion Coding.

Most of the time, we as developers want to know more things to do. Usually studying the brand new package used in Machine Learning or jumping fastly into our next challenge and this blocks any chance of improving what we have written.

When you start an interview it’s expected from you a correct behaviour. None of us would go into an interview wearing a tracksuit or presenting ourselves with a warm "Hey bro, what’s up?". In the same way, our code needs to meet some standards too.

Don’t worry if you have no idea on how to do it. You’ll learn it here. Your solution is Pylint.

What is Pylint?

It’s easy. Pylint is the fast way to improve your coding. It is a source code, bug and quality checker for Python.

It uses the PEP8 style guide. Don’t worry if you don’t know what it is. Pylint does it for you and quickly reports if you meet the needs.

Photo by Hunter Haley on Unsplash
Photo by Hunter Haley on Unsplash

Once you have seen the why let’s take a look at the how.

You can install it normally as if it was a common Python Package.

pip install pylint

Once you did this you already have pylint. The way to activate it’s like running a python script but instead of writting python you write pylint. That is:

pylint name_of_your_script.py

Let’s try an example

Here you have a very simple script what it does isn’t important. What matters is the structure and how to improve the style.

Let’s execute it and see how it goes.

Photo by the Author
Photo by the Author

Here is our mark: 6.15/10. Pretty good taking into account that there can be negative values too… (Ouch!)

Look at what we have now. The script’s name is followed by the line where the mistake appears. Having mistakes in the lines 2, 1, 8, 8 and 5.

Next is the description of the warnings. As you can see they are very self-explanatory. Here you have a list of typical common warnings:

  1. Exactly one space required after a comma
  2. Bad indentation (found 9 spaces, expected 8)
  3. Trailing whitespace (the line should not finish with whitespace)
  4. Final newline missing
  5. Missing docstrings (Piece of comment above module or function is missing, use it to describe functions, classes, modules in general) They have to be opened and closed "Docstring".
  6. Constant, function or class name does not satisfied needed format
  7. Either all return statements in a function should return an expression, or none of them should (what does defined function or class return? If there is nothing, return None)
  8. Maximum line length (line is longer than 100 characters)
  9. Snake-case naming style. By agreement in Python the different words in a class are indicated with PascalCase and in a function with kebab-case. For example: ThisIsAClass and this_is_a_function.

Now with our new information, you can try to improve our mark.

If you can’t do it here you have the correct code.

Don’t worry if it doesn’t go as wished the first time. Experience is a must!

Good luck!


Related Articles