Codementor Events

Python Style Guide - Complete

Published Nov 04, 2018Last updated May 02, 2019
Python Style Guide - Complete

I love Python and Pythonic code ❤

TL;DR,

Python Style Guide Repo

It is one of the first programming languages that I learned and started making a living writing it.

I have written a lot of Python code for different purposes at work and in my free time. Writing code that works is often fine, and you can live with it for some time. To me, however, this code should also be well written. It is always worth it to spend a few extra minutes to tidy and clean up your code before publishing it.

We read more code than we write and I personally like reading others' code. This exposed me to different styles of writing good Python code. The cleaner and more well-structured a code is, the easier it is to understand what it does. This is very important when you want to debug, refactor and optimise things. You simply can't do that with a code that you can't read and understand.

Python has an excellent style guide called PEP8. It covers most of the situations you will step into while writing Python. I like PEP8, and I make sure that my code is following PEP8 guidelines. I believe there has been much effort and thinking put into it. On the other hand, PEP8 can be considered a generic Python guideline as it allows different approaches to achieve similar goals.

Every time I faced a situation where I wasn't sure about the syntax, I would look it up on PEP8 to find that two or more ways are considered good. Being involved in big projects where more developers are daily pushing code, things tend to go wild concerning consistency.

I thought it would be a good idea to build an opinionated guide as an extension to PEP8. This should reduce the thinking about the right syntax and delegate the work to the guide.

You can find my Python Style Guide on GitHub. It is my effort at working toward my own default code style guide that I admire.

Feel free to use it, copy it, and modify it, so it fits you and your team. It is still a work in progress, therefore please excuse any mistakes. Your pull requests are more than welcome.

You shouldn't go crazy about following any guide and forcibly trying to apply it where it doesn't fit though. Style guides are made to help you focus on solving problems rather than being worried about the syntax.

In the next few posts here on Codementor, I will pick parts of the guide to discuss them. I hope by that I get feedback from other mentros and get to know their style in writing Python code.

Happy coding!

Discover and read more posts from Nour Wolf
get started
post commentsBe the first to share your opinion
Uğur Özyılmazel
5 years ago

link is broken… is it this one? https://github.com/nourchawich/python ?

Nour Wolf
5 years ago

yes!
thanks for pointing that out. For some reason, reference-style links stopped working as mentioned here

https://www.codementor.io/ianwang/codementor-writing-tutorials-markdown-cheat-sheet-lmpdjdriw#links

Show more replies