diff options
Diffstat (limited to 'sandbox/code-block-directive/docs/for-else-test.py.txt')
-rw-r--r-- | sandbox/code-block-directive/docs/for-else-test.py.txt | 83 |
1 files changed, 44 insertions, 39 deletions
diff --git a/sandbox/code-block-directive/docs/for-else-test.py.txt b/sandbox/code-block-directive/docs/for-else-test.py.txt index b5ba05848..d9687ecd5 100644 --- a/sandbox/code-block-directive/docs/for-else-test.py.txt +++ b/sandbox/code-block-directive/docs/for-else-test.py.txt @@ -1,5 +1,3 @@ -.. # -*- rst-mode -*- - Example for syntax highlight with Pygments ========================================== @@ -13,7 +11,13 @@ or to LaTeX with:: to gain syntax highlight in the output. -.. Run the doctests with ``pylit --doctest for-else-test.py``. +Convert between text <-> code source formats with:: + + pylit --code-block-marker='.. code-block:: python' + +Run the doctests with:: + + pylit --doctest for-else-test.py for-else-test @@ -25,63 +29,64 @@ First define a simple `for` loop. .. code-block:: python - def loop1(iterable): - """simple for loop with `else` statement""" - for i in iterable: - print i - else: - print "iterable empty" - print "Ende" - + def loop1(iterable): + """simple for loop with `else` statement""" + for i in iterable: + print i + else: + print "iterable empty" + print "Ende" + Now test it: The first test runs as I expect: iterator empty -> else clause applies: .. code-block:: pycon - >>> loop1(range(0)) - iterable empty - Ende - + >>> execfile('for-else-test.py') + >>> loop1(range(0)) + iterable empty + Ende + However, the else clause even runs if the iterator is not empty in the first place but after it is "spent": .. code-block:: pycon - >>> loop1(range(3)) - 0 - 1 - 2 - iterable empty - Ende - + >>> loop1(range(3)) + 0 + 1 + 2 + iterable empty + Ende + It seems like the else clause can only be prevented, if we break out of the loop. Let's try .. code-block:: python - def loop2(iterable): - """for loop with `break` and `else` statement""" - for i in iterable: - print i - break - else: - print "iterable empty" - print "Ende" - + def loop2(iterable): + """for loop with `break` and `else` statement""" + for i in iterable: + print i + break + else: + print "iterable empty" + print "Ende" + And indeed, the else clause is skipped after breaking out of the loop: .. code-block:: pycon - >>> loop2(range(3)) - 0 - Ende - + >>> loop2(range(3)) + 0 + Ende + The empty iterator runs as expected: .. code-block:: pycon - >>> loop2(range(0)) - iterable empty - Ende - + >>> loop2(range(0)) + iterable empty + Ende + |