| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -214,9 +214,9 @@ Why do lambdas defined in a loop with different values all return the same resul | |||
| 214 | 214 | Assume you use a for loop to define a few different lambdas (or even plain | |
| 215 | 215 | functions), e.g.:: | |
| 216 | 216 | ||
| 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) | ||
| 220 | 220 | ||
| 221 | 221 | This gives you a list that contains 5 lambdas that calculate ``x**2``. You | |
| 222 | 222 | 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:: | |||
| 241 | 241 | In order to avoid this, you need to save the values in variables local to the | |
| 242 | 242 | lambdas, so that they don't rely on the value of the global ``x``:: | |
| 243 | 243 | ||
| 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) | ||
| 247 | 247 | ||
| 248 | 248 | Here, ``n=x`` creates a new variable ``n`` local to the lambda and computed | |
| 249 | 249 | 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:: | |||
| 592 | 592 | Since the comma is not an operator, but a separator between expressions the | |
| 593 | 593 | above is evaluated as if you had entered:: | |
| 594 | 594 | ||
| 595 | - >>> ("a" in "b"), "a" | ||
| 595 | + ("a" in "b"), "a" | ||
| 596 | 596 | ||
| 597 | 597 | not:: | |
| 598 | 598 | ||
| 599 | - >>> "a" in ("b", "a") | ||
| 599 | + "a" in ("b", "a") | ||
| 600 | 600 | ||
| 601 | 601 | The same is true of the various assignment operators (``=``, ``+=`` etc). They | |
| 602 | 602 | 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 | |||
| 744 | 744 | unicode data, try using a :class:`io.StringIO` object or the :mod:`array` | |
| 745 | 745 | module:: | |
| 746 | 746 | ||
| 747 | + >>> import io | ||
| 747 | 748 | >>> s = "Hello, world" | |
| 748 | 749 | >>> sio = io.StringIO(s) | |
| 749 | 750 | >>> sio.getvalue() | |
@@ -761,7 +762,7 @@ module:: | |||
| 761 | 762 | array('u', 'Hello, world') | |
| 762 | 763 | >>> a[0] = 'y' | |
| 763 | 764 | >>> print(a) | |
| 764 | - array('u', 'yello world') | ||
| 765 | + array('u', 'yello, world') | ||
| 765 | 766 | >>> a.tounicode() | |
| 766 | 767 | 'yello, world' | |
| 767 | 768 | ||
@@ -1060,7 +1061,7 @@ How do I create a multidimensional list? | |||
| 1060 | 1061 | ||
| 1061 | 1062 | You probably tried to make a multidimensional array like this:: | |
| 1062 | 1063 | ||
| 1063 | - A = [[None] * 2] * 3 | ||
| 1064 | + >>> A = [[None] * 2] * 3 | ||
| 1064 | 1065 | ||
| 1065 | 1066 | This looks correct if you print it:: | |
| 1066 | 1067 | ||
@@ -1615,23 +1616,23 @@ file is automatic if you're importing a module and Python has the ability | |||
| 1615 | 1616 | (permissions, free space, etc...) to write the compiled module back to the | |
| 1616 | 1617 | directory. | |
| 1617 | 1618 | ||
| 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. | ||
| 1623 | 1624 | ||
| 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 | ||
| 1625 | 1626 | that is not imported -- you can, using the :mod:`py_compile` and | |
| 1626 | 1627 | :mod:`compileall` modules. | |
| 1627 | 1628 | ||
| 1628 | 1629 | The :mod:`py_compile` module can manually compile any module. One way is to use | |
| 1629 | 1630 | the ``compile()`` function in that module interactively:: | |
| 1630 | 1631 | ||
| 1631 | 1632 | >>> import py_compile | |
| 1632 | - >>> py_compile.compile('abc.py') | ||
| 1633 | + >>> py_compile.compile('foo.py') # doctest: +SKIP | ||
| 1633 | 1634 | ||
| 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 | ||
| 1635 | 1636 | override that with the optional parameter ``cfile``). | |
| 1636 | 1637 | ||
| 1637 | 1638 | You can also automatically compile all files in a directory or directories using | |
| Back | FazBrowse Home | New Git URL |
0 commit comments