Most rules from PEP-8 apply, except:Class names Imports Comments formatting Docstrings Long expressions and argument lists
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
- 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.
- General rules
- String quoting
- Variable names
- Abbreviations
- Decorators
- Magic method names
- 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
0 comments:
Post a Comment