PEP 214 - Why not print >> string?
Robert Amesz
sheershion at mailexpire.com
Fri Jan 11 10:14:30 EST 2002
Skip Montanaro wrote:
> Robert Amesz wrote:
>
>> But it makes a lot more sense to use a function which does
>> exactly the same as print and returns a string:
>>
>> def Print(*args):
>> return " ".join([str(x) for x in args])
>
> Well, more Pythonic it may be, but the semantics of two calls to
> your Print function aren't the same as executing two print
> statements. The caller would have to insert the required space or
> newline, depending on whether the print statement she isn't
> executing has a trailing comma or not.
True, this is only for single print statements. For multiple print
statements you'd need a writable object which behaves like a string.
Believe it or not, but such objects already exist, in the StringIO and
cStringIO modules.
So instead of:
s = ""
print >> s, "Something"
print >> s, "Something else"
you'd do:
import StringIO
sio = StringIO.StringIO()
print >> sio, "Something"
print >> sio, "Something else"
s = sio.getvalue()
sio.close() # or: del sio
Not counting the import statement, that's just two statements more than
in your proposal. As printing to a string isn't something which would
be used very often, I'd hardly say it warrants a language change.
Robert Amesz
More information about the Python-list
mailing list