🌐
Martin Thoma
martin-thoma.com › python-style-guide
Python Style Guide · Martin Thoma
The stuff in docs/ is often only for building HTML out of the Python code, organzinging things (e.g. which package to show first) and a user manual. There is PEP257 which defines some basic stuff. Building on this, there are two docstring style guides which cannot be combined: NumpyDoc an Google. Tools like napoleon in combination with Sphinx can automatically create nice docs of both of them. See GitHub for the guide. ... def get_meta(filepath, a_number, a_dict): """ Get meta-information of an image.
🌐
Python
peps.python.org › pep-0008
PEP 8 – Style Guide for Python Code | peps.python.org
Guido van Rossum <guido at python.org>, Barry Warsaw <barry at python.org>, Alyssa Coghlan <ncoghlan at gmail.com> ... This document gives coding conventions for the Python code comprising the standard library in the main Python distribution. Please see the companion informational PEP describing style guidelines for the C code in the C implementation of Python.
Discussions

Common coding style for Python? - Stack Overflow
I'm pretty new to Python, and I want to develop my first serious open source project. I want to ask what is the common coding style for python projects. I'll put also what I'm doing right now. 1.-... More on stackoverflow.com
🌐 stackoverflow.com
Google Python Style Guide
I don't agree with everything in there but 2 things I like about it. There's a pro/con/final decision for every subject and that is a good way to make up your own mind with all the information. Their docstring format (section 3.8). I just like the simplicity of it. More on reddit.com
🌐 r/Python
10
23
February 11, 2023
What are the main differences between PEP8 and Google Python Style Guide? - Stack Overflow
most notably they use two spaces for indentation instead of 4. The CamelCase style for functions and methods instead of the camel_case style. More on stackoverflow.com
🌐 stackoverflow.com
Style Guides for Python
The best way is to use a linter, e.g. pylint or flake8. What IDE do you use? VS Code will give you linting for free (i.e. with no extra effort) with the python extension. More on reddit.com
🌐 r/learnpython
26
107
March 18, 2020
🌐
Google
google.github.io › styleguide › pyguide.html
Google Style Guides | Style guides for Google-originated open-source projects
Python filenames must have a .py extension and must not contain dashes (-). This allows them to be imported and unittested. If you want an executable to be accessible without the extension, use a symbolic link or a simple bash wrapper containing exec "$0.py" "$@". For mathematically-heavy code, short variable names that would otherwise violate the style guide are preferred when they match established notation in a reference paper or algorithm.
🌐
Medium
vinayak-hegde.medium.com › a-comprehensive-style-guide-for-python-enhancing-readability-and-maintainability-5f893bdae85a
A Comprehensive Style Guide for Python: Enhancing Readability and Maintainability
December 14, 2023 - The Python community has widely adopted a style guide known as PEP 8, which stands for Python Enhancement Proposal 8. PEP 8 provides conventions for writing clean, readable code in Python.
🌐
GitHub
gist.github.com › 8059015
A style guide to use for Python code · GitHub
A style guide to use for Python code. GitHub Gist: instantly share code, notes, and snippets.
🌐
Django
docs.djangoproject.com › en › dev › internals › contributing › writing-code › coding-style
Coding style | Django documentation | Django
The class Meta should appear after the fields are defined, with a single blank line separating the fields and the class definition.
🌐
Pythonnative
pythonnative.com › meta › style-guide
Style Guide - PythonNative
Use Google-style docstrings everywhere (modules, classes, functions). Let type hints carry the types. Don't repeat them inside docstrings. Use Material admonitions (!!! tip "Title") for callouts in Markdown, not plain > blockquotes. Cross-link API symbols using mkdocstrings autorefs: [`use_state`][pythonnative.use_state]. Comments explain why, not what (the code already says what). Concepts, guides, examples, and API references describe PythonNative as it currently works.
🌐
Python Packaging
packaging.python.org › specifications › core-metadata
Core metadata specifications — Python Packaging User Guide
A longer description of the distribution that can run to several paragraphs. Software that deals with metadata should not assume any maximum size for this field, though people shouldn’t include their instruction manual as the description.
Find elsewhere
Top answer
1 of 3
22

PEP 8 is pretty much "the root" of all common style guides.

Google's Python style guide has some parts that are quite well thought of, but others are idiosyncratic (the two-space indents instead of the popular four-space ones, and the CamelCase style for functions and methods instead of the camel_case style, are pretty major idiosyncrasies).

On to your specific questions:

1.- What is the most widely used column width? (the eternal question) I'm currently sticking to 80 columns (and it's a pain!)

80 columns is most popular

2.- What quotes to use? (I've seen everything and PEP 8 does not mention anything clear) I'm using single quotes for everything but docstrings, which use triple double quotes.

I prefer the style you're using, but even Google was not able to reach a consensus about this:-(

3.- Where do I put my imports? I'm putting them at file header in this order.

import sys import -rest of python modules needed-

import whatever import -rest of application modules-

Yes, excellent choice, and popular too.

4.- Can I use "import whatever.function as blah"? I saw some documents that disregard doing this.

I strongly recommend you always import modules -- not specific names from inside a module. This is not just style -- there are strong advantages e.g. in testability in doing that. The as clause is fine, to shorten a module's name or avoid clashes.

5.- Tabs or spaces for indenting? Currently using 4 spaces tabs.

Overwhelmingly most popular.

6.- Variable naming style? I'm using lowercase for everything but classes, which I put in camelCase.

Almost everybody names classes with uppercase initial and constants with all-uppercase.

2 of 3
2

1.- Most everyone has a 16:9 or 16:10 monitor now days. Even if they don't have a wide-screen they have lots of pixels, 80 cols isn't a big practical deal breaker like it was when everyone was hacking at the command line in a remote terminal window on a 4:3 monitor at 320 X 240. I usually end the line when it gets too long, which is subjective. I am at 2048 X 1152 on a 23" Monitor X 2.

2.- Single quotes by default so you don't have to escape Double quotes, Double quotes when you need to embed single quotes, and Triple quotes for strings with embedded newlines.

3.- Put them at the top of the file, sometimes you put them in the main function if they aren't needed globally to the module.

4.- It is a common idiom to rename some modules. A good example is the following.

try:
    # for Python 2.6.x
    import json
except ImportError:
    # for previous Pythons
    try:
        import simplejson as json
    except ImportError:
        sys.exit('easy_install simplejson')

but the preferred way to import just a class or function is from module import xxx with the optional as yyy if needed

5.- Always use SPACES! 2 or 4 as long as no TABS

6.- Classes should up UpperCaseCamelStyle, variables are lowercase sometimes lowerCamelCase or sometimes all_lowecase_separated_by_underscores, as are function names. "Constants" should be ALL_UPPER_CASE_SEPARATED_BY_UNDERSCORES

When in doubt refer to the PEP 8, the Python source, existing conventions in a code base. But the most import thing is to be internally consistent as possible. All Python code should look like it was written by the same person when ever possible.

🌐
Luminous Men
luminousmen.com › post › 7-tips-to-make-an-effective-python-style-guide
7 tips to make an effective Python Style Guide
Meta Description: Discover 7 actionable tips to create a powerful Python Style Guide. Learn why a style guide is crucial, how to implement it effectively, and recommended tools to streamline the process.
🌐
Reddit
reddit.com › r/python › google python style guide
r/Python on Reddit: Google Python Style Guide
February 11, 2023 - My thoughts on docstrings, pdoc and Google style vs. Markdown ... The Move to Python 3 Begins!
🌐
Google
android.googlesource.com › platform › external › google-styleguide › + › 340543a160bdb7e3f6c9265aed81fb76c8e626e3 › pyguide.md
Google Python Style Guide
This line is used by the kernel to find the Python interpreter, but is ignored by Python when importing modules. It is only necessary on a file that will be executed directly. Be sure to use the right style for module, function, method docstrings and inline comments.
🌐
Luminous Men
luminousmen.com › post › the-ultimate-python-style-guidelines
The ultimate Python style guidelines
July 25, 2024 - Coding guidelines help engineering teams to write consistent code which is easy to read and understand for all team members. Python has an excellent style guide called PEP8. It covers most of the situations you will step into while writing Python.
🌐
Python
python.org › doc › essays › styleguide
Python Style Guide | Python.org
The style guide originally at this URL has been turned into two PEPs (Python Enhancement Proposals): PEP 8 for the main text, and PEP 257 for docstring conventions.
🌐
GitHub
github.com › ShenweiWang › google-styleguide › blob › master › pyguide.html
google-styleguide/pyguide.html at master · ShenweiWang/google-styleguide
<TITLE>Google Python Style Guide</TITLE> <META http-equiv="Content-Type" content="text/html; charset=utf-8"> <LINK HREF="http://www.google.com/favicon.ico" type="image/x-icon" rel="shortcut icon"> <LINK HREF="styleguide.css" type="text/css" rel="stylesheet"> <SCRIPT language="javascript" type="text/javascript"> ·
Author: ShenweiWang
🌐
Reddit
reddit.com › r/learnpython › style guides for python
r/learnpython on Reddit: Style Guides for Python
March 18, 2020 -

After some other people pointed out on my previous post that I was lacking a style guide for my code, I wanted to look into making my code more user friendly. I tried following along with the PEP 8 style guide on the python website, but I don’t feel like I am retaining any of the info. Would you be able to help me figure out ways to improve the style and redundancy of my code. Thank you

🌐
GitHub
github.com › hiling › google-styleguide › blob › master › pyguide.html
google-styleguide/pyguide.html at master · hiling/google-styleguide
<TITLE>Google Python Style Guide</TITLE> <META http-equiv="Content-Type" content="text/html; charset=utf-8"> <LINK HREF="http://www.google.com/favicon.ico" type="image/x-icon" rel="shortcut icon"> <LINK HREF="styleguide.css" type="text/css" rel="stylesheet"> <SCRIPT language="javascript" type="text/javascript"> ·
Author: hiling