[Tutor] print function (again)

Andrei Kulakov ak@silmarill.org
Sun, 21 Jul 2002 16:15:53 -0400


On Sun, Jul 21, 2002 at 12:15:57PM -0600, Erik Johnson wrote:
> On Wed, July 17 Guillermo Fernandez was asking about how to print without the automatic newline appended:
> 
> > I would like to do something like:
> >
> > print "first fct"
> > fct1()
> > print " second fct"
> > fct2()
> > print " third fct"
> > fct3()
> >
> > and have an output like:
> > "first fct second fct third fct"
> 
> 
> Steve (lonetwin@yahoo.com) responded by saying:
> 
>  You need to append a "," at the end for your string, thats all.
> ie:
>  print "first fct", 
>  fct1()
>  print "second fct",
>  .....
>  .....
> 
> will print
>  first fct second fct ....
> 	The comma will aslo take care of adding the space between strings that get 
> printed continuously.
> 
> 
> 
>     I, too, had the same question as Guillermo and thought I would share what I have figured out. The automatic newline is often appropriate and therefore often handy, but depending on what you are trying to do, it can equally as well be really obnoxious. If you intend to build up a string that is going to be separated by spaces then Steve's solution is dandy, but if you don't want spaces between what you are outputting in separate function calls, then "that simply don't work".
> 
> For example:
> 
> for x in range(5):
>   print x,
> 
> 0 1 2 3 4
> 
> 
> It does NOT print: "01234"
> 
> to get the string above, one solution is to call stdout's write() method directly:
> 
> import sys
> for x in range(5):
>     sys.stdout.write(x)
> 
> 01234
> 
> 
> to make this a little more convenient, you could always do something of this flavor:
> 
> >>> def printf(s):          # note that this does not handle variable argument lists
>         sys.stdout.write(s)

You could just do: printf = sys.stdout.write

> 
> >>> printf
> <function printf at 0x00A4F6D0>
> >>> for x in range(5):
>         printf(x)
> 
>  
> 01234
> >>> 
> 
>     An alternate approach is to simply build up a big string as you go, having your functions return strings rather than printing anything and then concatenating those together with whatever else you want to print and dumping it all at once. This may or may not be convenient.
> 
>     I think it is somewhat unfortunate that Python did not adopt a Pascal-like approach to this function such as print() giving "normal" print behaviour and println() for when you want an extra "\n" on the end. I am a PyNewbie and know that much of Python's internals are exposed, but I don't readily see a way to hack "print" because it seems to be handled differently (keyword?) than other functions. Contrast the following:

In my opinion, a language should have shortcuts for the most common use
paths. print is such a shortcut, because you mostly want to have a
space between printed things. I think you're simply accustomed to
pascal approach and that's why you see it as normal, a new user may as
well look at pascals' print not putting spaces in and think that very
odd and awkward.

> 
> >>> type(sys.stdout.write)
> <type 'instance method'>
> >>> sys.stdout.write
> <bound method PseudoFile.write of <PyShell.PseudoFile instance at 0x009F7168>>
> >>> type(print)
> SyntaxError: invalid syntax
> >>> print
> >>>
Yes, print is not a function, it's a keyword - the whole point of it is
being a shortcut, not having to write the (). I think this whole
mechanism of print and sys.stdout is very well thought out, although
sys.stdout trick should perhaps be mentioned more often in tutorials
because I remember seeing this question many many times..

 - Andrei
> 
> >>> 
> 
> 
>     Anyway, I hope that this helps and perhaps more advanced PyHackers would care to comment on subverting the print function for one's own devices. :)
> 
> -ej

-- 
Cymbaline: intelligent learning mp3 player - python, linux, console.
get it at: cy.silmarill.org