[Python-Dev] Replacement for print in Python 3.0

Jim Jewett jimjjewett at gmail.com
Fri Sep 2 02:12:11 CEST 2005


Guido van Rossum suggested:

>    stream.write(a1, a2, ...)
>    stream.writeln(a1, a2, ...)
>    stream.writef(fmt, a1, a2, ...)

> builtin functions write(), writeln(), writef() that call the
> corresponding method on sys.stdout. 

These seem good, except that write typically matches 
read, and I'm not sure how well the equivalents would
work.  (They can be defined; they just feel a little too
fragile, like C's input.)

> Another real problem with print is that, while the
> automatic insertion of spaces is nice for beginners,
> it often gets in the way, and what you have to do to 
> avoid this is pretty nasty: either drop print altogether 
> in favor of sys.stdout.write(), or use string concatenation
> or a format string, assuming you have all the pieces
> available at the same time (which often you don't).

I usually take "I need to get rid of spaces" as an indication
that I care about exact (not just readable, but exact) 
formatting, and *should* use either write or a format string 
(possibly waiting to collect the data).

Putting the spaces back in (without a format string) would 
be even worse.  Charles Cazabon's pointed out that it *could*
be as simple as

    writeln(' '.join( ... ))

but if there isn't a builtin alias, people (at least those not
intimidated by the magic required to get simple output)
*will* do things at least as bad as

    writeln(a, " ", b, " ", c)

or as bugprone as

    # oops, format string and debug vars got out of sync
    writef("  Current Vals:%s %d %d%s", curval, i, k, name, age)

-jJ


More information about the Python-Dev mailing list