P3 weird sys.stdout.write()

Diez B. Roggisch deets at nospam.web.de
Mon Aug 24 15:21:31 CEST 2009


Jerzy Jalocha N 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. 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 

>>> n = sys.stdout.write("")

instead, you won't see the behavior.

Diez



More information about the Python-list mailing list