This is a mirror of official site: http://jasper-net.blogspot.com/

Python Coding Style Guidelines

| Wednesday, January 19, 2011
Most rules from PEP-8 apply, except:

  • 79-character limit doesn’t really matter in some cases like long error or log messages, in those cases the limit is more like 100 chars. The code itself however should generally be restricted to 80-ish chars.
  • Some details on how to write comments, docstring, do imports and write abbreviations in class names.
  • Long expressions and argument list indentation rules.
The rules from Other Recommendations section of PEP-8 are mandatory. Don’t ever use semicolons or one-line if statements. On a related note, use if expressions only if they fit on one line.

  • General rules
  • String quoting
  • Variable names
  • Abbreviations
  • Decorators
  • Magic method names
  • Class names
  • Imports
  • Comments formatting
  • Docstrings
  • Long expressions and argument lists
    • Examples from real code
    • Exception to the “no newline escapes” rule

    General rules
    Many of these rules only apply when in doubt. It’s hard to read source if the code style varies for no reason, but having just 75% of code conform to the same rules makes it so much more pleasant to navigate. 100% compliance to the rules is not required and is simply unnecessary. Still, having a habit of writing certain constructs in the same way saves time even when writing it, and a lot more when reading.

    Also, whenever you can edit your source code not to require any comments — that’s much better than the best of comments or docstrings. Consider this:

    # check if user is allowed to edit
    if (user.logged_in and 'edit' in Role.get(user).permissions):
       ...
    and this:

    may_edit = user.logged_in and 'edit' in Role.get(user).permissions
    if may_edit:
       ...


    Read more: Python Coding Style Guidelines

    Posted via email from .NET Info

    0 comments: