P3 weird sys.stdout.write()

J

Jerzy Jalocha N

I've stumbled upon the following in Python 3:

Python 3.0.1+ (r301:69556, Apr 15 2009, 15:59:22)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
write() is appending the length of the string to it's output. That's
not how it worked in 2.6.

What's the reason for this? Is this intended? I couldn't find a bug
report for this.
 
D

Diez B. Roggisch

Jerzy said:
I've stumbled upon the following in Python 3:

Python 3.0.1+ (r301:69556, Apr 15 2009, 15:59:22)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
write() is appending the length of the string to it's output. That's
not how it worked in 2.6.

What's the reason for this? Is this intended? I couldn't find a bug
report for this.

Write returns the number of bytes written. And because you don't capture
that output into a variable, the interpreter puts it out as well.

If you do

instead, you won't see the behavior.

Diez
 
A

André

I've stumbled upon the following in Python 3:

Python 3.0.1+ (r301:69556, Apr 15 2009, 15:59:22)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
something9

write() is appending the length of the string to it's output.


Not quite right, see below.
That's
not how it worked in 2.6.

What's the reason for this? Is this intended? I couldn't find a bug
report for this.

I don't know what the reason for the change, but try the following:
5


What you are seeing is the concatenation of the return value of the
write() method with its output.

André
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,120
Latest member
ShelaWalli
Top