Note that there are some explanatory texts on larger screens.

plurals
  1. PODoctests: How to suppress/ignore output?
    text
    copied!<p>The doctest of the following (nonsense) Python module fails:</p> <pre><code>""" &gt;&gt;&gt; L = [] &gt;&gt;&gt; if True: ... append_to(L) # XXX &gt;&gt;&gt; L [1] """ def append_to(L): L.append(1) class A(object): pass return A() import doctest; doctest.testmod() </code></pre> <p>This is because the output after the line marked XXX is <code>&lt;__main__.A object at ...&gt;</code> (which is returned by <code>append_to</code>). Of course, I could put this output directly after the line marked XXX but in my case this would distract the reader from what shall be actually tested, namely the side effect of the function <code>append_to</code>. So how can I suppress that output or how can I ignore it. I tried it with:</p> <pre><code>""" &gt;&gt;&gt; L = [] &gt;&gt;&gt; if True: ... append_to(L) # doctest: +ELLIPSIS ... &gt;&gt;&gt; L [1] """ def append_to(L): L.append(1) class A(object): pass return A() import doctest; doctest.testmod() </code></pre> <p>However, this yields a <code>ValueError: line 4 of the docstring for __main__ has inconsistent leading whitespace: ' ...'</code>. </p> <p>What I don't want to do is to change the line <code>append_to(L)</code> to something like <code>_ = append_to(L)</code> which would suppress the output, because the doctest is for documentation purposes and to show the reader how the module is supposed to be used. (In the case being documented, <code>append_to</code> should be used statement-like and not like a function. Writing <code>_ = append_to(L)</code> would deviate the reader from this.)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload