Again: Please hear my plea: print without softspace

Martin Bless m.bless at gmx.de
Wed Mar 3 10:47:17 CET 2004


[David Goodger <goodger at python.org>]

>you should write a PEP.  Without a PEP, the idea will be forgotten.

Good advice, good encouragement, thanks.

I'd really like to. But unfortunately I've discovered the weak point
in my own argumentation: a new keyword could and probably would break
existing code. Therefore I can't go for a PEP until I have a better
idea.

Ok, then let's use some wrappers or helper functions. Here's the one I
currently like:

#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-
"""Do some experiments on write statements."""

__author__ = "Martin Bless, 2004-03-03"

class Writer:

    def __init__(self,writer=None):
        if writer is None:
            import sys
            self.writer = sys.stdout
        else:
            self.writer = writer

    def write(self,*args):
        for arg in args:
            self.writer.write(str(arg))

    __call__ = write

    def to(self,writer,*args):
        for arg in args:
            writer.write(str(arg))

write = Writer()
NL = '\n'
import sys
stderr = Writer(sys.stderr)

if __name__=="__main__":
    import sys
    outfile = sys.stdout or file('demo_tempfile','w')
    write('#', '-->'*10, NL)
    write('Starting.',NL)
    write(__doc__,NL)
    write("write(1,2,3,4,NL)=>", 1,2,3,4,NL)
    write("write('This',' ','is',' ','a',' ','demo.',NL)=>",
          'This',' ','is',' ','a',' ','demo.',NL)
    write.to( outfile,"write.to(outfile,5,6,7,8,NL)=>",5,6,7,8,NL)
    stderr("stderr('A number in parentheses: (', 77, ').', NL)=>",
           'A number in parentheses: (',77,').',NL)
    write('Done.',NL)
    write('#', '<--'*10, NL)

    """Output should be:
#-->-->-->-->-->-->-->-->-->-->
Starting.
Do some experiments on write statements.
write(1,2,3,4,NL)=>1234
write('This',' ','is',' ','a',' ','demo.',NL)=>This is a demo.
write.to(outfile,5,6,7,8,NL)=>5678
stderr('A number in parentheses: (',77,').',NL)=>A number in
parentheses: (77).
Done.
#<--<--<--<--<--<--<--<--<--<--
    """

This comes pretty close to what I was aiming at. But of course it's
just my version and different people will create different solutions.

mb - Martin Bless



More information about the Python-list mailing list