print()

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sat Oct 17 07:52:54 CEST 2009


En Sat, 17 Oct 2009 02:15:13 -0300, Grant Edwards  
<invalid at invalid.invalid> escribió:
> On 2009-10-17, Dave Angel <davea at ieee.org> wrote:
>> mattia wrote:
>>> Is there a way to print to an unbuffered output (like stdout)? I've  
>>> seen
>>> that something like sys.stdout.write("hello") works but it also prints
>>> the number of characters!
>>>
>>>
>> What the other responses (so far) didn't address is your comment about
>> "prints the number of characters."
>>
>> You're presumably testing this in the interpreter, which prints extra
>> stuff.  In particular, it prints the result value of any expressions
>> entered at the interpreter prompt.  So if you type
>>
>> sys.stdout.write("hello")
>>
>> then after the write() method is done, the return value of the method
>> (5) will get printed by the interpreter.
>
> Except sys.stdout.write("hello") doesn't return 5.  It returns
> None.
>
> I don't know what the OP is talking about when he says "prints
> the number of characters":
>
> $ python
> Python 2.6.2 (r262:71600, Aug 25 2009, 22:35:31)
> [GCC 4.3.2] on linux2
> Type "help", "copyright", "credits" or "license" for more
> information.
>>>> import sys
>>>> sys.stdout.write("hello\n")
> hello

Presumably he's using Python 3:

Python 3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit  
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
p3> import sys
p3> sys.stdout.write("hello")
hello5

See http://bugs.python.org/issue6345

-- 
Gabriel Genellina




More information about the Python-list mailing list