P3 weird sys.stdout.write()

André andre.roberge at gmail.com
Mon Aug 24 09:22:36 EDT 2009


On Aug 24, 10:13 am, Jerzy Jalocha N <jjalo... at gmail.com> wrote:
> 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.
>
> >>> import sys
> >>> sys.stdout.write("")
> 0
> >>> sys.stdout.write("something")
> 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:

>>> out = sys.stdout.write("test\n")
test
>>> out
5


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

André



More information about the Python-list mailing list