PEP 214 - Why not print >> string?

Aaron Swartz me at
Thu Jan 10 11:32:08 EST 2002

Hi all,

[ I'm not a careful follower of comp.lang.python so please cc any responses
to me. ]

I find the print statement a very convenient idiom for putting together
strings from a bunch of variables (something I seem to be doing a lot), but
I'm concerned that only allowing it to be used to append files (or file-like
objects) maybe be problematic.

For example, in PEP 214[1], the BDFL gives the example:

    def tables(n, file=None):
        for j in range(1, n+1):
            for i in range(1, n+1):
                print >> file, i, 'x', j, '=', i*j
            print >> file

It seems to me that this and other similar functions would be more
appropriate returning strings, so that their values can be used in other
ways and combined into expressions like len(tables(1)). (Yet still easily
written to a file as: "print >> file, tables(val)".) Of course, one can
always use a StringIO object, but this feels rather kludgey.

I'd like to suggest that one be able to provide a variable in addition to a
print statement. Thus, the tables function above could be rewritten as:

    def tables(n):
        output = ''
        for j in range(1, n+1):
            for i in range(1, n+1):
                print >> output, i, 'x', j, '=', i*j
            print >> output
        return output

This would be backwards compatible as currently >>ing to a string raises an
error. What do others thing about this?

Of course, I also strongly support PEP 215[2] (String Interpolation) and
would love to see it in the next version of Python. Having that would
probably make this change less necessary.


All the best,

[ "Aaron Swartz" ; <mailto:me at> ; <> ]

More information about the Python-list mailing list