The quest for the "definitive Python style guide"

Hey there,

Code style guidelines are an endless source of discussions and arguments among developers.

A while ago, newsletter member Andrew reached out to me with this question:

~~~

Hi Dan,

I've been enjoying your email updates and python tips! Thank you for doing this! I have a quick question. For someone new to Python, I'm trying to find the definitive style guide. A quick Google search turns up things like this:

https://www.python.org/dev/peps/pep-0008/
https://www.python.org/dev/peps/pep-0257/
https://google.github.io/styleguide/pyguide.html

Although it appears the first link has been updated, the first two seem rather old (2001)? Are these still the main go-to style guides? The next one looks to have been created by Google...but why would they do this if a definitive guide already exists?

As a contrast, the PHP-FIG page provides an excellent, and well laid-out recommended style guide: http://www.php-fig.org/psr/psr-1/

I guess I'm looking for the equivalent to that. I much appreciate any help you can offer. And again thank you for your emails and video tutorials!

~~~

One of the things I like the most about Python is it's high readability.

I strongly believe that "code is communication"—

And just like there are generally accepted stylistic rules & guidelines for written communication in English, there are code style guides for writing Python.

Sticking to these guidelines is one honking great idea.

They make it easier for development teams to work together. And even if you're working on a project by yourself, they'll help you make changes in the future.

My thinking there is this:

When you're reading code, your brain works like a Python compiler.

It first needs to parse the program text and then turn it into some internal representation before you can understand and modify the code.

If your code uses inconsistent styling and formatting rules, it trips up your brain's pattern recognizer and slows it down.

Therefore it takes longer to read and modify "sloppy" code.

And these "micro delays" add up and cost you productivity. They also make you feel exhausted quicker.

(I can't cite scientific proof for this theory, but from experience it's what I think happens. I talk about this a lot in my code review videos on YouTube.)

Alright, back to the original question, which sort of went like this:

"Okay great, but WHICH style guide should I use?"

Actually, the style guides Andrew mentioned are the most commonly used ones in Python.

Some people don't like Google's Python style guide because it has some "Google-isms" in it (how import statements are laid out etc.)

I'd say Google's guide is worth a read for a second opinion, but:

Generally the PEP 8 style guide is the preferred choice in the community and I also recommend and use it personally.

It's a great idea to mix PEP 8 with PEP 257 for docstring conventions. Those two make a great combo.

And yes, PEP 8 and PEP 257 are rather "well-seasoned"—which is NOT a bad thing.

These style guides have been maintained and fine-tuned for over a decade. Both are valuable resources that any Python developer worth their salt should know.

If you're ready to jump in but you're looking for an easier to read web-version of PEP 8 with improved formatting, check out pep8.org.

Happy Pythoning!

— Dan Bader

Older messages

New Python tutorials on Real Python

Saturday, June 20, 2020

Hey there, There's always something going on over at realpython.com as far as Python tutorials go. Here's what you may have missed this past week: Python Keywords: An Introduction Python

How to prepare for a Python coding interview

Sunday, June 14, 2020

Hey there, Participating in a "Silicon Valley style" coding interview can feel scary as heck. Unlike other professionals, it seems to be okay for software developers to expect to get

New Python tutorials on Real Python

Saturday, June 13, 2020

Hey there, There's always something going on over at realpython.com as far as Python tutorials go. Here's what you may have missed this past week: Python Community Interview With Kattni Rembor

New Python tutorials on Real Python

Saturday, June 6, 2020

Hey there, There's always something going on over at realpython.com as far as Python tutorials go. Here's what you may have missed this past week: Build Physical Projects With Python on the

I'm certified

Friday, June 5, 2020

Hey there, One of the "shameful secrets" of my career is that I'm a certified "Scrum Product Owner." Let me tell you how this came to be: One fine Tuesday morning my manager

You Might Also Like

Welcome to How-To Geek Newsletters

Thursday, November 28, 2024

How-To Geek Logo Hi, Welcome to the How-To Geek newsletters! We're delighted to have you as part of our community! Our newsletter includes highlights of the best stories, instructive how-to manuals

💻 Issue 438 - Hidden NET 9 gems

Thursday, November 28, 2024

This week's Awesome .NET Weekly Read this email on the Web The Awesome .NET Weekly Issue » 438 Release Date Nov 28, 2024 Your weekly report of the most popular .NET news, articles and projects

💎 Issue 445 - Rails is better low code than low code

Thursday, November 28, 2024

This week's Awesome Ruby Newsletter Read this email on the Web The Awesome Ruby Newsletter Issue » 445 Release Date Nov 28, 2024 Your weekly report of the most popular Ruby news, articles and

💻 Issue 445 - Deno vs. Oracle: Canceling the JavaScript Trademark

Thursday, November 28, 2024

This week's Awesome JavaScript Weekly Read this email on the Web The Awesome JavaScript Weekly Issue » 445 Release Date Nov 28, 2024 Your weekly report of the most popular JavaScript news, articles

📱 Issue 439 - Google's iOS app now injects links on third-party websites that go to Search

Thursday, November 28, 2024

This week's Awesome iOS Weekly Read this email on the Web The Awesome iOS Weekly Issue » 439 Release Date Nov 28, 2024 Your weekly report of the most popular iOS news, articles and projects Popular

💻 Issue 363 - 7 challenges to do before a React interview

Thursday, November 28, 2024

This week's Awesome React Weekly Read this email on the Web The Awesome React Weekly Issue » 363 Release Date Nov 28, 2024 Your weekly report of the most popular React news, articles and projects

💻 Issue 445 - Mastering JavaScript Event Delegation

Thursday, November 28, 2024

This week's Awesome Node.js Weekly Read this email on the Web The Awesome Node.js Weekly Issue » 445 Release Date Nov 28, 2024 Your weekly report of the most popular Node.js news, articles and

📱 Issue 442 - Static, Dynamic, Mergeable, oh, my!

Thursday, November 28, 2024

This week's Awesome Swift Weekly Read this email on the Web The Awesome Swift Weekly Issue » 442 Release Date Nov 28, 2024 Your weekly report of the most popular Swift news, articles and projects

💻 Issue 440 - Nio: An Async Runtime for Rust

Thursday, November 28, 2024

This week's Awesome Rust Weekly Read this email on the Web The Awesome Rust Weekly Issue » 440 Release Date Nov 28, 2024 Your weekly report of the most popular Rust news, articles and projects

Data Science Weekly - Issue 575

Thursday, November 28, 2024

Curated news, articles and jobs related to Data Science, AI, & Machine Learning ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏