print with no newline

Peter Otten __peter__ at web.de
Sat Sep 4 02:06:53 EDT 2004


Paul Watson wrote:

> Many thanks for pointing out File.softspace attribute.  However, I get
> mixed
> results when using it.  I am sure there is some logic to it somewhere.  It
> does not appear to control the end of line.  The online doc says that it
> controls putting a space -before- another value.  The

The softspace is normally set when a string is printed but cleared when a
newline is encountered. The print statement uses it to determine whether a
space should precede the string it is about to write. By clearing it
manually you can omit the space between two strings:

>>> import sys
>>> print "abc",;sys.stdout.softspace=0;print "def"
abcdef

When the program is terminated, the flag is (ab)used to determine whether a
line was started but not finished. Only then it controls whether a newline
is printed or not. My original idea was to register an exit handler that
does that, but it turned out that would be too late.

> File.softspace.__doc__
> string appears to need review also.  

The softspace attribute is just an ordinary integer, i. e. you get the same
docstring you get for any int instance - the docstring of the int class:

>>> int.__doc__ == 42 .__doc__ # note the space after 42
True

> I think I am ready to use File.write() and move on.

No objections here :-)

Peter




More information about the Python-list mailing list