StringIO in 2.6 and beyond

Peter Otten __peter__ at web.de
Tue Dec 9 20:26:38 CET 2008


Bill McClain wrote:

> On 2008-12-09, Peter Otten <__peter__ at web.de> wrote:
> 
>> >>> out = io.StringIO()
>> >>> print(u"hello", file=out, end=u"\n")
>> >>> out.getvalue()
>> u'hello\n'
> 
> That has the benefit of working. Thank you!
> 
> That can't be the intended behavior of print(), can it? Insering
> non-unicode spaces and line terminators? I thought all text was unicode
> now. Or is that only in 3.0?

Yes it's 3.0 only. I have no clear idea of the implications of using the
print() function in 2.6 yet; maybe changing the defaults for end/sep to
unicode would suffice to make it work smoothly. I will probably defer the
transition from statement to function until I move to 3.x.

One benefit of the function is that you can do things like

from functools import partial
print = partial(print, sep=u" ", end=u"\n")

Peter



More information about the Python-list mailing list