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

Software Testing Weekly - Issue 218

Friday, May 3, 2024

Unit, Integration and End-to-End Tests 🔧 View on the Web Archives ISSUE 218 May 4th 2024 COMMENT Welcome to the 218th issue! I loved going through this discussion among software engineers: What is your

gpt2-chatbot and OpenAI search engine - Weekly News Roundup - Issue #465

Friday, May 3, 2024

Plus: Med-Gemini; Vidu - Chinese answer to OpenAI's Sora; the first race of Abu Dhabi Autonomous Racing League; deepfaking celebrities to teach math and physics; and more! ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏

NASA comes to the rescue of crowded rocket launch sites

Friday, May 3, 2024

Plus: Fisker's legal woes and Sprinklr lays off 100 View this email online in your browser By Christine Hall Friday, May 3, 2024 Good afternoon, and welcome to TechCrunch PM. We made it to Friday,

🎮 Forget the PS5 Pro, I Still Love My PS4 — The Best Lock Screen Widgets for iPhone

Friday, May 3, 2024

Also: Smart Home Mistakes to Avoid, and More! How-To Geek Logo May 3, 2024 Did You Know Half of the world's geysers are located in Yellowstone National Park. 🔑 More Passkeys Happy Friday! You can

JSK Daily for May 3, 2024

Friday, May 3, 2024

JSK Daily for May 3, 2024 View this email in your browser A community curated daily e-mail of JavaScript news The Power of React's Virtual DOM: A Comprehensive Explanation Modern JavaScript

Musk raises $6B for AI startup

Friday, May 3, 2024

Also, is TikTok dodging Apple's commissions? View this email online in your browser By Haje Jan Kamps Friday, May 3, 2024 Welcome to Startups Weekly — Haje's weekly recap of everything you can

SWLW #597: Seek first to understand, The "Iterative Adjacent Possible", and more.

Friday, May 3, 2024

Weekly articles & videos about people, culture and leadership: everything you need to design the org that makes the product. A weekly newsletter by Oren Ellenbogen with the best content I found

iOS Dev Weekly - Issue 659

Friday, May 3, 2024

Is Swift 6 hitting one of the REAL hard problems? Not generics, not data race safety, but naming things! 😬 View on the Web Archives ISSUE 659 May 3rd 2024 Comment Naming things is one of the two hard

Daily Coding Problem: Problem #1430 [Easy]

Friday, May 3, 2024

Daily Coding Problem Good morning! Here's your coding interview problem for today. This problem was asked by Facebook. You have a large array with most of the elements as zero. Use a more space-

Making sense of product management

Friday, May 3, 2024

​ Getting a sense of product sense Whenever I hear the term product sense, I think back to a Seinfeld episode about write-offs (with a little artistic license). Jerry: “You don't even know what