PEP 214 - Why not print >> string?

Steve Holden sholden at holdenweb.com
Thu Jan 10 12:16:01 EST 2002


"Aaron Swartz" <me at aaronsw.com> wrote in message
news:B8631E26.17D53%me at aaronsw.com...
> 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.
>
Unfortunately, this would seem to require that a variable name be bound to a
string value in order to indicate that the result of the print statement
should overwrite it. Then the prints are rebinding the named variable
(obviously, since the string values are immutable). This seems a little
bizarre.

Why not Perl? ;-)

regards
 Steve
--
http://www.holdenweb.com/








More information about the Python-list mailing list