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


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

Interactive entered code, inserts spurious numbers.

Started byAntoon Pardon <antoon.pardon@rece.vub.ac.be>
First post2015-07-31 10:47 +0200
Last post2015-07-31 19:30 +1000
Articles 2 — 2 participants

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


Contents

  Interactive entered code, inserts spurious numbers. Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-07-31 10:47 +0200
    Re: Interactive entered code, inserts spurious numbers. Steven D'Aprano <steve@pearwood.info> - 2015-07-31 19:30 +1000

#94808 — Interactive entered code, inserts spurious numbers.

FromAntoon Pardon <antoon.pardon@rece.vub.ac.be>
Date2015-07-31 10:47 +0200
SubjectInteractive entered code, inserts spurious numbers.
Message-ID<mailman.1114.1438332507.3674.python-list@python.org>
I'm using python 3.4.2 on debian 8.

This is the code:

==== 8< =====

import sys
write = sys.stdout.write

from math import pi

frac = 3
for a in range(2 * frac):
    write("%2d: %6.4f\n" % (a, a * pi / frac))

===== 8< ====

Now when this code is written in a file and executed
I get the expected result:

 0: 0.0000
 1: 1.0472
 2: 2.0944
 3: 3.1416
 4: 4.1888
 5: 5.2360

But when I enter this code interactively in the interpreter
I get the following result:

 0: 0.0000
11
 1: 1.0472
11
 2: 2.0944
11
 3: 3.1416
11
 4: 4.1888
11
 5: 5.2360
11

That is different behaviour from python2, which gives me
the expected result. My guess is that the write returns
11, being the number of characters written en that the
interpreter, shows that each time through the loop.

But is this the expected behaviour in python3? I find
it annoying.

[toc] | [next] | [standalone]


#94810

FromSteven D'Aprano <steve@pearwood.info>
Date2015-07-31 19:30 +1000
Message-ID<55bb402d$0$1657$c3e8da3$5496439d@news.astraweb.com>
In reply to#94808
On Fri, 31 Jul 2015 06:47 pm, Antoon Pardon wrote:

> That is different behaviour from python2, which gives me
> the expected result. My guess is that the write returns
> 11, being the number of characters written en that the
> interpreter, shows that each time through the loop.

In Python 3, write returns the number of characters written (or bytes
written, when in binary mode).

At the interactive interpreter, results returned but not assigned to
anything are printed.

> But is this the expected behaviour in python3? I find
> it annoying.

Yes, expected.

    dontcare = write("%2d: %6.4f\n" % (a, a * pi / frac))



-- 
Steven

[toc] | [prev] | [standalone]


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


csiph-web