Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7ba05d8

Browse files
author
Mark Pilgrim
committed
add var to catch return value of sys.stdout.write
1 parent 6305d1a commit 7ba05d8

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

files.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -464,20 +464,20 @@ <h2 id=stdio>Standard Input, Output, and Error</h2>
464464

465465
<pre class=screen>
466466
<samp class=p>>>> </samp><kbd class=pp>for i in range(3):</kbd>
467-
<a><samp class=p>... </samp><kbd class=pp> print('PapayaWhip')</kbd> <span class=u>&#x2460;</span></a>
467+
<a><samp class=p>... </samp><kbd class=pp> print('PapayaWhip')</kbd> <span class=u>&#x2460;</span></a>
468468
<samp>PapayaWhip
469469
PapayaWhip
470470
PapayaWhip</samp>
471471
<samp class=p>>>> </samp><kbd class=pp>import sys</kbd>
472472
<samp class=p>>>> </samp><kbd class=pp>for i in range(3):</kbd>
473-
<a><samp class=p>... </samp><kbd class=pp> sys.stdout.write('is the')</kbd> <span class=u>&#x2461;</span></a>
473+
<a><samp class=p>... </samp><kbd class=pp> l = sys.stdout.write('is the')</kbd> <span class=u>&#x2461;</span></a>
474474
<samp>is theis theis the</samp>
475475
<samp class=p>>>> </samp><kbd class=pp>for i in range(3):</kbd>
476-
<a><samp class=p>... </samp><kbd class=pp> sys.stderr.write('new black')</kbd> <span class=u>&#x2462;</span></a>
476+
<a><samp class=p>... </samp><kbd class=pp> l = sys.stderr.write('new black')</kbd> <span class=u>&#x2462;</span></a>
477477
<samp>new blacknew blacknew black</samp></pre>
478478
<ol>
479479
<li>The <code>print()</code> function, in a loop. Nothing surprising here.
480-
<li><code>stdout</code> is defined in the <code>sys</code> module, and it is a <a href=#file-like-objects>stream object</a>. Calling its <code>write()</code> function will print out whatever string you give it. In fact, this is what the <code>print</code> function really does; it adds a carriage return to the end of the string you&#8217;re printing, and calls <code>sys.stdout.write</code>.
480+
<li><code>stdout</code> is defined in the <code>sys</code> module, and it is a <a href=#file-like-objects>stream object</a>. Calling its <code>write()</code> function will print out whatever string you give it, then return the length of the output. In fact, this is what the <code>print</code> function really does; it adds a carriage return to the end of the string you&#8217;re printing, and calls <code>sys.stdout.write</code>.
481481
<li>In the simplest case, <code>sys.stdout</code> and <code>sys.stderr</code> send their output to the same place: the Python <abbr>IDE</abbr> (if you&#8217;re in one), or the terminal (if you&#8217;re running Python from the command line). Like standard output, standard error does not add carriage returns for you. If you want carriage returns, you&#8217;ll need to write carriage return characters.
482482
</ol>
483483

0 commit comments

Comments
 (0)