Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: pages/coding-style.html

Issue 29747647: Noissue - Document best practice for formatting multiline statements in Python (Closed)
Patch Set: Incorporate Julian's suggestions Created April 12, 2018, 12:38 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | static/css/main.css » ('j') | static/css/main.css » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pages/coding-style.html
===================================================================
--- a/pages/coding-style.html
+++ b/pages/coding-style.html
@@ -60,16 +60,48 @@
<li>{{python-general Follow <a href="https://www.python.org/dev/peps/pep-0008/"><fix>PEP-8</fix></a> and the recommendations in the offical <fix>Python</fix> documentation.}}</li>
<li>{{python-version Make the code compatible with both <fix>Python 2.7</fix> and <fix>Python 3.5+</fix> (see <a href="https://docs.python.org/dev/howto/pyporting.html">this guide</a>). Use <a href="https://docs.python.org/2/library/__future__.html"><fix>__future__</fix> imports</a> to address syntactic differences but avoid <a href="https://pythonhosted.org/six/"><fix>six</fix></a>, <a href="http://python-future.org/compatible_idioms.html"><fix>python-future</fix></a>, etc. to not introduce additional dependencies.}}</li>
<li>{{python-strings Write string literals so that they match the behaviour of <a href="https://docs.python.org/3/library/functions.html#ascii"><code><fix>ascii()</fix></code></a>, i.e. use single quotes except to avoid escaping of embedded quotes and use <code><fix>\u</fix></code> escapes for non-ascii characters but don't prefix strings with <code><fix>u</fix></code>. For docstrings, however, follow <a href="https://www.python.org/dev/peps/pep-0257/">PEP-257</a>.}}</li>
<li>{{python-prefix In modules, prefix private functions and variables with a single underscore.}}</li>
<li>{{python-concatenation Use the <code><fix>+</fix></code> operator when concatenating exactly two strings, use the <a href="https://docs.python.org/2/library/stdtypes.html#str.format"><code><fix>format()</fix></code> method</a> for more complex string formatting, use the <a href="https://docs.python.org/2/library/stdtypes.html#str.join"><code><fix>join()</fix></code> method</a> when concatenating pre-existing sequences.}}</li>
<li>{{python-tuple-vs-list Use tuples for data that have structure, use lists for data that have order.}}</li>
<li>{{python-builtins Don't override builtins except for <a href="https://docs.python.org/2/library/functions.html#non-essential-built-in-funcs">non-essential builtins</a> and <code><fix>file</fix></code> which is superfluos in modern code as well.}}</li>
<li>{{python-map-filter Use list comprehensions or generator expressions instead of calling <code><fix>map()</fix></code> or <code><fix>filter()</fix></code> with a lambda function.}}</li>
+ <li>{{python-multiline The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses:}}
+ <ol>
+ <li>{{python-multiline-a1[list item] aligning follow up lines with the opening parentheses and putting the closing parentheses at the end of the last line}}
+ (<a onclick="getElementById('python-multiline-a1-example').classList.toggle('hide');">{{python-multiline-a1-example example}}</a>), or
+ <pre class="hide" id="python-multiline-a1-example">
+ long_object_name.long_method_name(argument1, argument2,
+ keyword_argument=value)
+
+ very_long_variable = ['a list of', 'strings that does not',
+ 'fit into', 'one line']
+ </pre>
+ </li>
+ <li>{{python-multiline-a2[list item] breaking the line at the opening parentheses, indenting follow up lines by 4 spaces, and placing the closing parentheses on a separate line that is aligned with the line that contains the opening parentheses}}
+ (<a onclick="getElementById('python-multiline-a2-example').classList.toggle('hide');">{{python-multiline-a2-example example}}</a>).
+ <pre class="hide" id="python-multiline-a2-example">
+ long_object_name.long_method_name(
+ argument1,
+ argument2,
+ keyword_argument=value,
+ )
+
+ very_long_variable = [
+ 'a list of',
+ 'strings that does not',
+ 'fit into',
+ 'one line',
+ ]
+ </pre>
+ </li>
+ </ol>
+ {{python-multiline-commas When breaking lists of items separated by commas (e.g. lists, dicts, argument lists) using the second approach add a comma after the last element. This allows adding and rearranging items without touching unrelated lines.}}
+ </li>
<li>{{python-regexp Use <a href="https://docs.python.org/2/library/re.html#re.search"><code><fix>re.search()</fix></code></a> instead of <code><fix>re.match()</fix></code> to avoid <a href="https://docs.python.org/2/library/re.html#search-vs-match">confusion</a> about implied beginning of the string but not the ending.}}</li>
<li>
{{python-flake8 Run <a href="https://pypi.python.org/pypi/flake8"><fix>flake8</fix></a> with following extensions and fix all reported errors:}}
<ul>
<li><a href="https://hg.adblockplus.org/codingtools/file/tip/flake8-eyeo">flake8-eyeo</a></li>
<li><a href="https://pypi.python.org/pypi/flake8-docstrings">flake8-docstrings</a> ({{python-flake8-docstring-exception with <code><fix>D1</fix></code> ignored}})</li>
<li><a href="https://pypi.python.org/pypi/pep8-naming">pep8-naming</a></li>
</ul>
« no previous file with comments | « no previous file | static/css/main.css » ('j') | static/css/main.css » ('J')

Powered by Google App Engine
This is Rietveld