Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #60300 > unrolled thread

Re: sys.stdout and Python3

Started byChris Angelico <rosuav@gmail.com>
First post2013-11-24 01:16 +1100
Last post2013-11-25 01:39 +1100
Articles 3 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: sys.stdout and Python3 Chris Angelico <rosuav@gmail.com> - 2013-11-24 01:16 +1100
    Re: sys.stdout and Python3 Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-24 14:31 +0000
      Re: sys.stdout and Python3 Chris Angelico <rosuav@gmail.com> - 2013-11-25 01:39 +1100

#60300 — Re: sys.stdout and Python3

FromChris Angelico <rosuav@gmail.com>
Date2013-11-24 01:16 +1100
SubjectRe: sys.stdout and Python3
Message-ID<mailman.3084.1385216188.18130.python-list@python.org>
On Sun, Nov 24, 2013 at 12:26 AM, Frank Millman <frank@chagford.com> wrote:
> for i in range(10):
>   sys.stdout.write('.')
>   sys.stdout.flush()
>   time.sleep(1)
> sys.stdout.write('\n')
>
> I tried it under Python3, and found that it differs in two ways -
>
> 1. Each 'write' is terminated by a newline
> 2. Each 'write' appends the length of the string written.

Only in the interactive interpreter, where return values get printed.
In a script, that won't happen. To prevent that from happening
interactively, just assign the result to something:

for i in range(10):
    _=sys.stdout.write(".")
    sys.stdout.flush()

There definitely is a difference between Py2 and Py3 there, but it's
nothing to do with sys.stdout - it's a change in the REPL (interactive
interpreter, Read/Eval/Print Loop) and how it handles return values
inside loops. I think it's an improvement, overall, though it is a
little confusing when you work with partial output.

ChrisA

[toc] | [next] | [standalone]


#60376

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-11-24 14:31 +0000
Message-ID<52920dd3$0$29993$c3e8da3$5496439d@news.astraweb.com>
In reply to#60300
On Sun, 24 Nov 2013 01:16:18 +1100, Chris Angelico wrote:

> On Sun, Nov 24, 2013 at 12:26 AM, Frank Millman <frank@chagford.com>
> wrote:
>> for i in range(10):
>>   sys.stdout.write('.')
>>   sys.stdout.flush()
>>   time.sleep(1)
>> sys.stdout.write('\n')
>>
>> I tried it under Python3, and found that it differs in two ways -
>>
>> 1. Each 'write' is terminated by a newline 2. Each 'write' appends the
>> length of the string written.
> 
> Only in the interactive interpreter, where return values get printed. In
> a script, that won't happen. To prevent that from happening
> interactively, just assign the result to something:
> 
> for i in range(10):
>     _=sys.stdout.write(".")
>     sys.stdout.flush()
> 
> There definitely is a difference between Py2 and Py3 there, but it's
> nothing to do with sys.stdout - it's a change in the REPL (interactive
> interpreter, Read/Eval/Print Loop) and how it handles return values
> inside loops. I think it's an improvement, overall, though it is a
> little confusing when you work with partial output.


I don't think the REPL handles return values inside loops any different 
from how it handles them outside loops. The difference is that file.write 
methods used to return None in Python 2, in Python 3 they return the 
number of bytes written.


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#60379

FromChris Angelico <rosuav@gmail.com>
Date2013-11-25 01:39 +1100
Message-ID<mailman.3137.1385303985.18130.python-list@python.org>
In reply to#60376
On Mon, Nov 25, 2013 at 1:31 AM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> I don't think the REPL handles return values inside loops any different
> from how it handles them outside loops. The difference is that file.write
> methods used to return None in Python 2, in Python 3 they return the
> number of bytes written.

Oh! That would explain it, thanks for clarifying. So my justification
was wrong, though the solutions (assign the return value to something,
or run it in a script rather than the REPL) still apply.

ChrisA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web