[Python-Dev] extended print statement, uPre-PEP

Tim Peters tim_one@email.msn.com
Sun, 23 Jul 2000 18:23:48 -0400


[Barry A. Warsaw]
> ...
> log.write('post to %s from %s, size=%d\n' % (listname,
>                                              sender, len(msg)))
>
> vs
>
> print 'post to', listname, 'from', sender, 'size=', len(msg)
>
> With the former, you have to know about the rules for string
> interpolation, you have to make sure you've got your tuple is the
> right length, you have to remember to add the trailing newline.

OTOH, they don't produce the same output!  The comma after "from %s" is
missing in the latter, and the latter adds an extra space after "size=".
These very much relate to the endlessly repeated newcomer questions Paul
sketched, such as "how can I get rid of the trailing newline?", "how can I
print to a different file?", and (IIRC, Paul missed this one) "how can I get
*rid* of the space between two items?".

The uncomfortable truth is that getting the output you want, to the
destination you want, is usually a lot less hassle in Perl.  Paul latched on
to the heart of Perl's convenience with his

[Paul]
> log.writeln($"post to ${listname} from ${sender}, size=${len(msg)}")

where I've put back the comma from Barry's original.  Perhaps Barry will
counter with

    print >> log, $"post to ${listname} from ${sender}, size=${len(msg)}"

Then I'd counter-counter <wink> with

    print >> log, $"post to $listname from $sender, size=${len(msg)}"

But there's not a new suggestion in this whole thing -- even ">>"
specifically was suggested long ago for this purpose.

I can't say whether Guido ever read those suggestions, but I don't recall
him ever responding.  ABC *did* have auto-stringifying expression
interpolation in its strings, so presumably he didn't like it there.

I know I didn't like it in ABC, but for an avoidable reason:  it used the
same character for both "interpolate this" left and right brackets, which
often made it hard to see exactly what was being interpolated.  I've always
been surprised that Guido retained that latter property in Python's
shorthand for repr (a function that doesn't *merit* a shorthand if ever
there was one <wink>):

>>> print ``1+2`+`3+4``
'37'
>>>

I'm not sure how Python's parser manages to get that straight!  In
Precodese, it would be (except that $(x) is presumably str(x) rather than
repr(x)):

    print $"$($(1+2)+$(3+4))"

which at least makes the grouping clear.  I'm all for it.

in-principle-if-not-in-every-detail-ly y'rs  - tim