FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Merge: Tweak programming faq examples so that it (mostly) passes doct… · python/cpython@2f72aed · GitHub

Repository navigation

Commit 2f72aed

Browse files
committed
Merge: Tweak programming faq examples so that it (mostly) passes doctest.
2 parents 15a7d2e + fdf9503 commit 2f72aed

1 file changed

Lines changed: 19 additions & 18 deletions

File tree

‎Doc/faq/programming.rst‎

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,9 @@ Why do lambdas defined in a loop with different values all return the same resul
214214
Assume you use a for loop to define a few different lambdas (or even plain
215215
functions), e.g.::
216216

217-
squares = []
218-
for x in range(5):
219-
squares.append(lambda: x**2)
217+
>>> squares = []
218+
>>> for x in range(5):
219+
... squares.append(lambda: x**2)
220220

221221
This gives you a list that contains 5 lambdas that calculate ``x**2``. You
222222
might expect that, when called, they would return, respectively, ``0``, ``1``,
@@ -241,9 +241,9 @@ changing the value of ``x`` and see how the results of the lambdas change::
241241
In order to avoid this, you need to save the values in variables local to the
242242
lambdas, so that they don't rely on the value of the global ``x``::
243243

244-
squares = []
245-
for x in range(5):
246-
squares.append(lambda n=x: n**2)
244+
>>> squares = []
245+
>>> for x in range(5):
246+
... squares.append(lambda n=x: n**2)
247247

248248
Here, ``n=x`` creates a new variable ``n`` local to the lambda and computed
249249
when the lambda is defined so that it has the same value that ``x`` had at
@@ -592,11 +592,11 @@ Comma is not an operator in Python. Consider this session::
592592
Since the comma is not an operator, but a separator between expressions the
593593
above is evaluated as if you had entered::
594594

595-
>>> ("a" in "b"), "a"
595+
("a" in "b"), "a"
596596

597597
not::
598598

599-
>>> "a" in ("b", "a")
599+
"a" in ("b", "a")
600600

601601
The same is true of the various assignment operators (``=``, ``+=`` etc). They
602602
are not truly operators but syntactic delimiters in assignment statements.
@@ -744,6 +744,7 @@ it from. However, if you need an object with the ability to modify in-place
744744
unicode data, try using a :class:`io.StringIO` object or the :mod:`array`
745745
module::
746746

747+
>>> import io
747748
>>> s = "Hello, world"
748749
>>> sio = io.StringIO(s)
749750
>>> sio.getvalue()
@@ -761,7 +762,7 @@ module::
761762
array('u', 'Hello, world')
762763
>>> a[0] = 'y'
763764
>>> print(a)
764-
array('u', 'yello world')
765+
array('u', 'yello, world')
765766
>>> a.tounicode()
766767
'yello, world'
767768

@@ -1060,7 +1061,7 @@ How do I create a multidimensional list?
10601061

10611062
You probably tried to make a multidimensional array like this::
10621063

1063-
A = [[None] * 2] * 3
1064+
>>> A = [[None] * 2] * 3
10641065

10651066
This looks correct if you print it::
10661067

@@ -1615,23 +1616,23 @@ file is automatic if you're importing a module and Python has the ability
16151616
(permissions, free space, etc...) to write the compiled module back to the
16161617
directory.
16171618

1618-
Running Python on a top level script is not considered an import and no ``.pyc``
1619-
will be created. For example, if you have a top-level module ``abc.py`` that
1620-
imports another module ``xyz.py``, when you run abc, ``xyz.pyc`` will be created
1621-
since xyz is imported, but no ``abc.pyc`` file will be created since ``abc.py``
1622-
isn't being imported.
1619+
Running Python on a top level script is not considered an import and no
1620+
``.pyc`` will be created. For example, if you have a top-level module
1621+
``foo.py`` that imports another module ``xyz.py``, when you run ``foo``,
1622+
``xyz.pyc`` will be created since ``xyz`` is imported, but no ``foo.pyc`` file
1623+
will be created since ``foo.py`` isn't being imported.
16231624

1624-
If you need to create abc.pyc -- that is, to create a .pyc file for a module
1625+
If you need to create ``foo.pyc`` -- that is, to create a ``.pyc`` file for a module
16251626
that is not imported -- you can, using the :mod:`py_compile` and
16261627
:mod:`compileall` modules.
16271628

16281629
The :mod:`py_compile` module can manually compile any module. One way is to use
16291630
the ``compile()`` function in that module interactively::
16301631

16311632
>>> import py_compile
1632-
>>> py_compile.compile('abc.py')
1633+
>>> py_compile.compile('foo.py') # doctest: +SKIP
16331634

1634-
This will write the ``.pyc`` to the same location as ``abc.py`` (or you can
1635+
This will write the ``.pyc`` to the same location as ``foo.py`` (or you can
16351636
override that with the optional parameter ``cfile``).
16361637

16371638
You can also automatically compile all files in a directory or directories using

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL