Skip to content

Commit cf5bdfc

Browse files
authored
Stan's suggestions for programming FAQ update (#80)
2 parents a8a0d97 + 166b130 commit cf5bdfc

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

Doc/faq/programming.rst

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ executables:
103103
* `py2app <https://github.com/ronaldoussoren/py2app>`_ (macOS only)
104104
* `py2exe <https://www.py2exe.org/>`_ (Windows only)
105105

106+
106107
Are there coding standards or a style guide for Python programs?
107108
----------------------------------------------------------------
108109

@@ -143,7 +144,7 @@ results in an :exc:`!UnboundLocalError`:
143144
>>> foo()
144145
Traceback (most recent call last):
145146
...
146-
UnboundLocalError: local variable 'x' referenced before assignment
147+
UnboundLocalError: cannot access local variable 'x' where it is not associated with a value
147148

148149
This is because when you make an assignment to a variable in a scope, that
149150
variable becomes local to that scope and shadows any similarly named variable
@@ -300,7 +301,7 @@ It's good practice if you import modules in the following order:
300301

301302
1. standard library modules -- such as :mod:`sys`, :mod:`os`, :mod:`argparse`, :mod:`re`
302303
2. third-party library modules (anything installed in Python's site-packages
303-
directory) -- such as :mod:`!dateutil`, :mod:`!requests`, :mod:`!PIL.Image`
304+
directory) -- such as :pypi:`dateutil`, :pypi:`requests`, :pypi:`tzdata`
304305
3. locally developed modules
305306

306307
It is sometimes necessary to move imports to a function or class to avoid
@@ -714,8 +715,8 @@ not::
714715

715716
"a" in ("b", "a")
716717

717-
The same is true of the various assignment operators (``=``, ``+=`` etc). They
718-
are not truly operators but syntactic delimiters in assignment statements.
718+
The same is true of the various assignment operators (``=``, ``+=``, and so on).
719+
They are not truly operators but syntactic delimiters in assignment statements.
719720

720721

721722
Is there an equivalent of C's "?:" ternary operator?
@@ -1066,13 +1067,14 @@ the raw string::
10661067

10671068
Also see the specification in the :ref:`language reference <strings>`.
10681069

1070+
10691071
Performance
10701072
===========
10711073

10721074
My program is too slow. How do I speed it up?
10731075
---------------------------------------------
10741076

1075-
That's a tough one, in general. First, here are a list of things to
1077+
That's a tough one, in general. First, here is list of things to
10761078
remember before diving further:
10771079

10781080
* Performance characteristics vary across Python implementations. This FAQ
@@ -1125,6 +1127,7 @@ yourself.
11251127
The wiki page devoted to `performance tips
11261128
<https://wiki.python.org/moin/PythonSpeed/PerformanceTips>`_.
11271129

1130+
11281131
.. _efficient_string_concatenation:
11291132

11301133
What is the most efficient way to concatenate many strings together?
@@ -1324,7 +1327,7 @@ Or, you can use an extension that provides a matrix datatype; `NumPy
13241327
How do I apply a method or function to a sequence of objects?
13251328
-------------------------------------------------------------
13261329

1327-
To call a method or function and accumulate the return values is a list,
1330+
To call a method or function and accumulate the return values in a list,
13281331
a :term:`list comprehension` is an elegant solution::
13291332

13301333
result = [obj.method() for obj in mylist]
@@ -1340,6 +1343,7 @@ a plain :keyword:`for` loop will suffice::
13401343
for obj in mylist:
13411344
function(obj)
13421345

1346+
13431347
.. _faq-augmented-assignment-tuple-error:
13441348

13451349
Why does a_tuple[i] += ['item'] raise an exception when the addition works?
@@ -1444,7 +1448,7 @@ How can I sort one list by values from another list?
14441448
----------------------------------------------------
14451449

14461450
Merge them into an iterator of tuples, sort the resulting list, and then pick
1447-
out the element you want. ::
1451+
out the element you want.
14481452

14491453
>>> list1 = ["what", "I'm", "sorting", "by"]
14501454
>>> list2 = ["something", "else", "to", "sort"]
@@ -1775,6 +1779,7 @@ to use private variable names at all.
17751779
The :ref:`private name mangling specifications <private-name-mangling>`
17761780
for details and special cases.
17771781

1782+
17781783
My class defines __del__ but it is not called when I delete the object.
17791784
-----------------------------------------------------------------------
17801785

@@ -1884,9 +1889,9 @@ are preferred. In particular, identity tests should not be used to check
18841889
constants such as :class:`int` and :class:`str` which aren't guaranteed to be
18851890
singletons::
18861891

1887-
>>> a = 1000
1888-
>>> b = 500
1889-
>>> c = b + 500
1892+
>>> a = 10_000_000
1893+
>>> b = 5_000_000
1894+
>>> c = b + 5_000_000
18901895
>>> a is c
18911896
False
18921897

@@ -1955,9 +1960,9 @@ parent class:
19551960

19561961
.. testcode::
19571962

1958-
from datetime import date
1963+
import datetime as dt
19591964

1960-
class FirstOfMonthDate(date):
1965+
class FirstOfMonthDate(dt.date):
19611966
"Always choose the first day of the month"
19621967
def __new__(cls, year, month, day):
19631968
return super().__new__(cls, year, month, 1)

0 commit comments

Comments
 (0)