[Python-checkins] cpython (2.7): Tweak programming faq examples so that it (mostly) passes doctest.

r.david.murray python-checkins at python.org
Wed Jun 19 23:01:11 CEST 2013


http://hg.python.org/cpython/rev/6f535c725b27
changeset:   84223:6f535c725b27
branch:      2.7
parent:      84207:a5ef439f3c9e
user:        R David Murray <rdmurray at bitdance.com>
date:        Wed Jun 19 17:00:43 2013 -0400
summary:
  Tweak programming faq examples so that it (mostly) passes doctest.

Back port of 0113247f894b from 3.3.

The exceptions are the import related questions at the end, which
need to be rewritten anyway, and a math example that doesn't
exist in the 3.3+ docs that I didn't bother trying to fix.

files:
  Doc/faq/programming.rst |  41 ++++++++++++++--------------
  1 files changed, 21 insertions(+), 20 deletions(-)


diff --git a/Doc/faq/programming.rst b/Doc/faq/programming.rst
--- a/Doc/faq/programming.rst
+++ b/Doc/faq/programming.rst
@@ -360,9 +360,9 @@
 Assume you use a for loop to define a few different lambdas (or even plain
 functions), e.g.::
 
-   squares = []
-   for x in range(5):
-      squares.append(lambda: x**2)
+   >>> squares = []
+   >>> for x in range(5):
+   ...    squares.append(lambda: x**2)
 
 This gives you a list that contains 5 lambdas that calculate ``x**2``.  You
 might expect that, when called, they would return, respectively, ``0``, ``1``,
@@ -387,9 +387,9 @@
 In order to avoid this, you need to save the values in variables local to the
 lambdas, so that they don't rely on the value of the global ``x``::
 
-   squares = []
-   for x in range(5):
-      squares.append(lambda n=x: n**2)
+   >>> squares = []
+   >>> for x in range(5):
+   ...    squares.append(lambda n=x: n**2)
 
 Here, ``n=x`` creates a new variable ``n`` local to the lambda and computed
 when the lambda is defined so that it has the same value that ``x`` had at
@@ -748,11 +748,11 @@
 Since the comma is not an operator, but a separator between expressions the
 above is evaluated as if you had entered::
 
-    >>> ("a" in "b"), "a"
+    ("a" in "b"), "a"
 
 not::
 
-    >>> "a" in ("b", "a")
+    "a" in ("b", "a")
 
 The same is true of the various assignment operators (``=``, ``+=`` etc).  They
 are not truly operators but syntactic delimiters in assignment statements.
@@ -897,6 +897,7 @@
 You can't, because strings are immutable.  If you need an object with this
 ability, try converting the string to a list or use the array module::
 
+   >>> import io
    >>> s = "Hello, world"
    >>> a = list(s)
    >>> print a
@@ -910,7 +911,7 @@
    >>> print a
    array('c', 'Hello, world')
    >>> a[0] = 'y' ; print a
-   array('c', 'yello world')
+   array('c', 'yello, world')
    >>> a.tostring()
    'yello, world'
 
@@ -1172,7 +1173,7 @@
 
 You probably tried to make a multidimensional array like this::
 
-   A = [[None] * 2] * 3
+   >>> A = [[None] * 2] * 3
 
 This looks correct if you print it::
 
@@ -1740,13 +1741,13 @@
 (permissions, free space, etc...) to write the compiled module back to the
 directory.
 
-Running Python on a top level script is not considered an import and no ``.pyc``
-will be created.  For example, if you have a top-level module ``abc.py`` that
-imports another module ``xyz.py``, when you run abc, ``xyz.pyc`` will be created
-since xyz is imported, but no ``abc.pyc`` file will be created since ``abc.py``
-isn't being imported.
-
-If you need to create abc.pyc -- that is, to create a .pyc file for a module
+Running Python on a top level script is not considered an import and no
+``.pyc`` will be created.  For example, if you have a top-level module
+``foo.py`` that imports another module ``xyz.py``, when you run ``foo``,
+``xyz.pyc`` will be created since ``xyz`` is imported, but no ``foo.pyc`` file
+will be created since ``foo.py`` isn't being imported.
+
+If you need to create ``foo.pyc`` -- that is, to create a ``.pyc`` file for a module
 that is not imported -- you can, using the :mod:`py_compile` and
 :mod:`compileall` modules.
 
@@ -1754,9 +1755,9 @@
 the ``compile()`` function in that module interactively::
 
    >>> import py_compile
-   >>> py_compile.compile('abc.py')
-
-This will write the ``.pyc`` to the same location as ``abc.py`` (or you can
+   >>> py_compile.compile('foo.py')                 # doctest: +SKIP
+
+This will write the ``.pyc`` to the same location as ``foo.py`` (or you can
 override that with the optional parameter ``cfile``).
 
 You can also automatically compile all files in a directory or directories using

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list