PEP 214 - Why not print >> string?
Alex Martelli
aleax at aleax.it
Thu Jan 10 11:40:35 EST 2002
"Aaron Swartz" <me at aaronsw.com> wrote in message
news:B8631E26.17D53%me at aaronsw.com...
...
> 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
...
> 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?
I think it would be an utter disaster, and in that sense quite compatible
indeed with "print >>". Have "print >> foo, something" rebind name foo if
foo names a string (and build up the resulting string by the disasterous
equivalent of +=) is, in my opinion, a terrible idea.
def tables(n):
format = "%d x %d = %d"
output = []
for j in range(1, n+1):
for i in range(1, n+1):
output.append(format % (i, j, i*j))
output.append('')
return '\n'.join(output)
that's how I'd code it myself...
Alex
More information about the Python-list
mailing list