Please hear my plea: print without softspace
Robin Becker
robin at jessikat.fsnet.co.uk
Sat Feb 28 12:35:20 EST 2004
In article <slrnc41i7e.81f.npat at localhost.localdomain>, Nick Patavalis
<npat at efault.net> writes
>In article <edc2QLAGMJQAFwoM at jessikat.fsnet.co.uk>, Robin Becker wrote:
>>
>>
>> ##############
>> class hidesoftspace:
>> def __init__(self,fobj):
>> self.__dict__['_fobj'] = fobj
>> fobj.softspace = False
>> def __getattr__(self,a):
>> if a=='softspace': return False
>> return getattr(self._fobj,a)
>> def __setattr__(self,a,v):
>> if a=='softspace': return
>> setattr(self._fobj,a,v)
>>
>> import sys
>> print 'before hiding',1,2,3,4
>> sys.stdout=hidesoftspace(sys.stdout)
>> print 'after hiding',1,2,3,4
>> ##############
>
>Would you perhaps care to comment a bit more, or provide a pointer to
>some docs? It seems that there is a "softspace" atribute in the
>sys.stdout file object, and that this attribute gets set by the print
>statement itself. With your clever trick you make it imposible for
>someone to set this attribute to anything else but "False". What are
>the exact semantics behind this?
>
>I tried this:
>
> import sys
> print sys.stdout.softspace
> 0
> print "test:", 1, 2, 3, sys.stdout.softspace
> test: 1 2 3 1
> print sys.stdout.softspace
> 0
>
>Which seems to support my explanation, but what exactly are the
>semantics of "softspace"?
>
>Thanks
>/npat
>
>
OK I'll try. I believe softspace is a boolean attribute of the standard
file class which is used by print to indicate that a space should be
inserted in the stream before the next item. Logically it should always
be false at the beginning of a print sequence and true after the print
of an item.
You have to be careful testing this as prints at the prompt don't behave
properly ie you always get a linefeed in the command terminal.
try
from sys import stdout
print stdout.softspace,stdout.softspace,;print stdout.softspace
should give
0 1 1
my class just ensures that a wrapped (warped :)) file will never have
its softspace set to true and thus we won't get the extra spaces; in
short print always thinks the wrapped file is at the start of a print
sequence.
-softspaced in the head-ly yrs-
Robin Becker
More information about the Python-list
mailing list