[Python-Dev] Replacement for print in Python 3.0

Nick Coghlan ncoghlan at gmail.com
Sun Sep 4 04:08:37 CEST 2005

Barry Warsaw wrote:
> On Sat, 2005-09-03 at 11:17, Guido van Rossum wrote:
>>I see two different ways to support the two most-called-for additional
>>requirements: (a) an option to avoid the trailing newline, (b) an
>>option to avoid the space between items.
> See a (very quick and very dirty ;) strawman that I just posted to the
> wiki.  I think this has some interesting semantics, including the
> ability to control the separator inline in a C++-like fashion.  The
> writef() version also accepts string.Templates or %s-strings as its
> first argument.  I'm not sure I like reserving 'to' and 'nl' keyword
> arguments, and not having the ability to print Separator instances
> directly, but OTOH maybe those aren't big deals.

The latter problem is easily solved by calling str() at the point of the call
so that write() never sees the actual Separator object. However, this 'inline'
behaviour modification has always annoyed me in C++ - if you want this kind of
control over the formatting, a format string is significantly clearer. I think
your own examples from the Wiki page show this:

write('obj:', obj, 'refs:', refs)
write(Separator(': '), 'obj', obj,
       Separator(', '), 'refs',
       Separator(': '), refs,

writef('obj: %s, refs: %s', obj, refs)
writef(Template('obj: $obj, refs: $refs, obj: $obj'),
        obj=obj, refs=refs,

That said, looking at 'writef' suggests a different solution to me - a builtin
called 'format'. The latter two examples would become:

write(format('obj: %s, refs: %s', obj, refs))
write(format(Template('obj: $obj, refs: $refs, obj: $obj'),
              obj=obj, refs=refs),

Separating the formatting out into a separate functions like this addresses
your concern with the namespace conflict for 'to' and 'nl', and also makes the
'format' builtin more generally useful, as it can be used for cases other than
direct output to a stream.


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-Dev mailing list