[Python-Dev] Replacement for print in Python 3.0

Guido van Rossum guido at python.org
Tue Sep 6 16:21:09 CEST 2005

On 9/6/05, Nick Coghlan <ncoghlan at gmail.com> wrote:
> I did a fair bit of tinkering with that on the weekend. Printing a sequence of
> strings is fine - it's the call to "map(str, seq)" that makes printing a
> sequence of non-strings uglier than it should be. Doing it that way also
> breaks the Python idiom of letting unicode remain as unicode.

Only of you insist on doing it in a single call. With an explicit for
loop all is well.

> However, one thing I eventually remembered was a discussion about a year ago
> regarding the possibility of allowing str.join and unicode.join to accept
> non-strings [1].
> That discussion ended up leaving the join methods alone, because it is damn
> hard to do it without slowing down the common case where the sequence is all
> strings.
> I'm currently considering proposing a "joinany" method for str and unicode
> which accepts a sequence of arbitrary objects (I have a patch, but it needs
> unit tests and docs, and I'm uncomfortable with the amount of code duplication
> between the join and joinany methods).

Why not take an idea that Fredrik Lundh mentioned earlier, and have a
built-in *function* named join() which takes a sequence and a string?
joinany() is an ugly name.

But what's still missing is a use case analysis where you prove that
the use case is common enough to require explicit support.

> This becomes especially clear once "sorted" and "list.sort" are given as
> examples where the various keyword arguments do not change the basic invariant
> properties of the sorting operations - you start with a sequence, and you end
> up with essentially the same sequence, only in a different order. The keyword
> arguments simply control the precise meaning of "different order".

Thanks -- a very good example!

> 'printraw' is good - it makes it clear it is part of the same family as
> 'print' and 'printf', and explains succintly how it differs from the normal
> print function.

(Except that 'raw' could mean anything.)

> Additionally, doing 'printraw' with 'printf' is a little tricky - the best
> I've come up with is "printf('%s'*3, a, b, c)".

Yeah, but often the real code you need to do is already written as

  print("x =", x, "y =", y, "z =", z)

and that becomes more readable when you transform it to

  printf("x = %s y = %s z = %s\n", x, y, z)

--Guido van Rossum (home page: http://www.python.org/~guido/)

More information about the Python-Dev mailing list