Re: print()

Discussion in 'Python' started by Grant Edwards, Oct 17, 2009.

  1. On 2009-10-17, Dave Angel <> 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
    >>>
    >>>


    > Either put the statement in a real script, or do the following trick to
    > convince yourself:
    >
    > dummy = sys.stdout.write("hello")


    I don't see why the assignment is needed.

    --
    Grant
     
    Grant Edwards, Oct 17, 2009
    #1
    1. Advertising

  2. En Sat, 17 Oct 2009 02:15:13 -0300, Grant Edwards
    <> escribió:
    > On 2009-10-17, Dave Angel <> 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
     
    Gabriel Genellina, Oct 17, 2009
    #2
    1. Advertising

  3. Grant Edwards

    Dave Angel Guest

    Grant Edwards wrote:
    > On 2009-10-17, Dave Angel <> wrote:
    >
    > <snip>
    > 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":
    >
    >

    You're right of course, I should have just tried it. I must have been
    thinking of read(), or maybe write() in some other language. But I know
    I've seen some problem like this, where the value the interpreter
    printed was mixed with what the user wanted.
    > <snip>
    >
    >> Either put the statement in a real script, or do the following trick to
    >> convince yourself:
    >>
    >> dummy = sys.stdout.write("hello")
    >>

    >
    > I don't see why the assignment is needed.
    >
    >

    Only if write() had returned a value other than None, which is of course
    not how it works, nor how it's documented.
     
    Dave Angel, Oct 17, 2009
    #3
  4. Grant Edwards

    Terry Reedy Guest

    Gabriel Genellina wrote:

    > Presumably he's using Python 3:


    And apparently not IDLE

    > 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


    IDLE

    Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit
    (Intel)] on win32

    >>> import sys
    >>> sys.stdout.write('abc')

    abc

    reported, for better or worse, in http://bugs.python.org/issue7163

    Since interactive users do not usually use sys.stdout.write (versus
    print), the mixed output is not usually a problem.

    tjr
     
    Terry Reedy, Oct 17, 2009
    #4
  5. Grant Edwards

    Mark Tolonen Guest

    "Terry Reedy" <> wrote in message
    news:hbdh51$g0f$...
    > Gabriel Genellina wrote:
    >
    >> Presumably he's using Python 3:

    >
    > And apparently not IDLE
    >
    >> 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

    >
    > IDLE
    >
    > Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit (Intel)]
    > on win32
    >
    > >>> import sys
    > >>> sys.stdout.write('abc')

    > abc
    >
    > reported, for better or worse, in http://bugs.python.org/issue7163
    >
    > Since interactive users do not usually use sys.stdout.write (versus
    > print), the mixed output is not usually a problem.
    >
    > tjr


    Apparently, Pythonwin has the same "problem":

    PythonWin 3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit
    (Intel)] on win32.
    Portions Copyright 1994-2008 Mark Hammond - see 'Help/About PythonWin' for
    further copyright information.
    >>> import sys
    >>> sys.stdout.write('hello')

    hello
    >>> print(sys.stdout.write('hello'))

    helloNone

    -Mark
     
    Mark Tolonen, Oct 18, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bad_Kid
    Replies:
    3
    Views:
    3,956
    Shawn H. Mesiatowsky
    Apr 7, 2005
  2. Bryan Shaw

    Lock Print Screen and File Print

    Bryan Shaw, Apr 19, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    3,958
    Kevin Spencer
    Apr 20, 2005
  3. EDom
    Replies:
    0
    Views:
    443
  4. keto
    Replies:
    0
    Views:
    980
  5. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    366
    David Cournapeau
    Dec 30, 2008
Loading...

Share This Page