You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/writing/style.rst
+25-21Lines changed: 25 additions & 21 deletions
Original file line number
Diff line number
Diff line change
@@ -101,7 +101,7 @@ calls to ``send('Hello', 'World')`` and ``point(1, 2)``.
101
101
102
102
2. **Keyword arguments** are not mandatory and have default values. They are often
103
103
used for optional parameters sent to the function. When a function has more than
104
-
two or three positional parameters, its signature will be more difficult to remember
104
+
two or three positional parameters, its signature is more difficult to remember
105
105
and using keyword argument with default values is helpful. For instance, a more
106
106
complete ``send`` function could be defined as ``send(message, to, cc=None, bcc=None)``.
107
107
Here ``cc`` and ``bcc`` are optional, and evaluate to ``None`` when they are not
@@ -176,15 +176,15 @@ possible to do each of the following:
176
176
177
177
However, all these options have many drawbacks and it is always better to use
178
178
the most straightforward way to achieve your goal. The main drawback is that
179
-
readability suffers deeply from them. Many code analysis tools, such as pylint
180
-
or pyflakes, will be unable to parse this "magic" code.
179
+
readability suffers greatly when using these constructs. Many code analysis
180
+
tools, such as pylint or pyflakes, will be unable to parse this "magic" code.
181
181
182
182
We consider that a Python developer should know about these nearly infinite
183
-
possibilities, because it grows the confidence that no hard-wall will be on the
184
-
way. However, knowing how to use them and particularly when **not** to use
185
-
them is the most important.
183
+
possibilities, because it instills confidence that no impassable problem will
184
+
be on the way. However, knowing how and particularly when **not** to use
185
+
them is very important.
186
186
187
-
Like a Kungfu master, a Pythonista knows how to kill with a single finger, and
187
+
Like a kung fu master, a Pythonista knows how to kill with a single finger, and
188
188
never to actually do it.
189
189
190
190
We are all consenting adults
@@ -195,10 +195,10 @@ dangerous. A good example is that any client code can override an object's
195
195
properties and methods: there is no "private" keyword in Python. This
196
196
philosophy, very different from highly defensive languages like Java, which
197
197
give a lot of mechanisms to prevent any misuse, is expressed by the saying: "We
198
-
are consenting adults".
198
+
are all consenting adults".
199
199
200
200
This doesn't mean that, for example, no properties are considered private, and
201
-
that no proper encapsulation is possible in Python. But, instead of relying on
201
+
that no proper encapsulation is possible in Python. Rather, instead of relying on
202
202
concrete walls erected by the developers between their code and other's, the
203
203
Python community prefers to rely on a set of conventions indicating that these
204
204
elements should not be accessed directly.
@@ -210,8 +210,8 @@ the code is modified is the responsibility of the client code.
210
210
211
211
Using this convention generously is encouraged: any method or property that is
212
212
not intended to be used by client code should be prefixed with an underscore.
213
-
This will guarantee a better separation of duties and easier modifications of
214
-
existing code, and it will always be possible to publicize a private property,
213
+
This will guarantee a better separation of duties and easier modification of
214
+
existing code; it will always be possible to publicize a private property,
215
215
while privatising a public property might be a much harder operation.
216
216
217
217
Returning values
@@ -235,7 +235,7 @@ statement can assume the condition is met to further compute the function's main
235
235
Having multiple such return statements is often necessary.
236
236
237
237
However, when a function has multiple main exit points for its normal course, it becomes
238
-
difficult to debug the returned result, and it may be preferable to keep a single exit
238
+
difficult to debug the returned result, so it may be preferable to keep a single exit
239
239
point. This will also help factoring out some code paths, and the multiple exit points
240
240
are a probable indication that such a refactoring is needed.
241
241
@@ -281,7 +281,7 @@ a tuple of two elements for each item in list:
281
281
for index, item inenumerate(some_list):
282
282
# do something with index and item
283
283
284
-
You can use this to swap variables, as well:
284
+
You can use this to swap variables as well:
285
285
286
286
.. code-block:: python
287
287
@@ -360,9 +360,13 @@ Take the following code for example:
360
360
deflookup_list(l):
361
361
return's'in l
362
362
363
-
Even though both functions look identical, because *lookup_dict* is utilizing the fact that dictionaries in Python are hashtables, the lookup performance between the two is very different.
364
-
Python will have to go through each item in the list to find a matching case, which is time consuming. By analysing the hash of the dictionary, finding keys in the dict can be done very quickly.
365
-
For more information see this `StackOverflow <http://stackoverflow.com/questions/513882/python-list-vs-dict-for-look-up-table>`_ page.
363
+
Even though both functions look identical, because *lookup_dict* is utilizing
364
+
the fact that dictionaries in Python are hashtables, the lookup performance
365
+
between the two is very different. Python will have to go through each item
366
+
in the list to find a matching case, which is time consuming. By analysing
367
+
the hash of the dictionary, finding keys in the dict can be done very quickly.
368
+
For more information see this `StackOverflow <http://stackoverflow.com/questions/513882/python-list-vs-dict-for-look-up-table>`_
369
+
page.
366
370
367
371
Zen of Python
368
372
-------------
@@ -406,7 +410,7 @@ PEP 8
406
410
407
411
Conforming your Python code to PEP 8 is generally a good idea and helps make
408
412
code more consistent when working on projects with other developers. There
409
-
exists a command-line program, `pep8 <https://github.com/jcrocholl/pep8>`_,
413
+
is a command-line program, `pep8 <https://github.com/jcrocholl/pep8>`_,
410
414
that can check your code for conformance. Install it by running the following
411
415
command in your Terminal:
412
416
@@ -584,19 +588,19 @@ files for you.
584
588
print line
585
589
586
590
The ``with`` statement is better because it will ensure you always close the
587
-
file, even if an exception is raised.
591
+
file, even if an exception is raised inside the ``with`` block.
588
592
589
593
Line Continuations
590
594
~~~~~~~~~~~~~~~~~~
591
595
592
596
When a logical line of code is longer than the accepted limit, you need to
593
-
split it over multiple physical lines. Python interpreter will join consecutive
597
+
split it over multiple physical lines. The Python interpreter will join consecutive
594
598
lines if the last character of the line is a backslash. This is helpful
595
-
sometimes but is preferably avoided, because of its fragility: a white space
599
+
in some cases, but should usually be avoided because of its fragility: a white space
596
600
added to the end of the line, after the backslash, will break the code and may
597
601
have unexpected results.
598
602
599
-
A preferred solution is to use parentheses around your elements. Left with an
603
+
A better solution is to use parentheses around your elements. Left with an
600
604
unclosed parenthesis on an end-of-line the Python interpreter will join the
601
605
next line until the parentheses are closed. The same behavior holds for curly
0 commit comments