print "hello", >> file

Peter Hansen peter at engcorp.com
Tue Feb 25 12:04:14 EST 2003


phil hunt wrote:
> 
> Wouldn't it be nice if appending to a file, appending to a string
> and appending to stdout had the same syntax?
> 
>    f = file("somefilename", "w")
>    f << "hello"
> 
>    s = "some string"
>    s << "hello"
> 
>    out << "hello"

Since you can't append to a string, clearly you meant something
different in the second case.  Maybe StringIO?

Python 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit (Intel)] on win32
>>> f = file("somefile", "w")
>>> print >>f, "hello"

>>> import StringIO
>>> s = StringIO.StringIO()
>>> print >>s, "hello"

>>> import sys
>>> out = sys.stdout
>>> print >>out, "hello"
hello

Yes, it is nice that all three methods support the same syntax, 
wouldn't it?  ;-)

-Peter




More information about the Python-list mailing list