pre-PEP: Print Without Intervening Space

Bengt Richter bokr at oz.net
Fri Mar 11 16:50:57 EST 2005


On Fri, 11 Mar 2005 10:59:16 -0500, Steve Holden <steve at holdenweb.com> wrote:

>Marcin Ciura wrote:
>> Duncan Booth wrote:
>> 
>>> import sys
>>> def nospace(value, stream=None):
>>>     '''Suppress output of space before printing value'''
>>>     stream = stream or sys.stdout
>>>     stream.softspace = 0
>>>     return str(value)
>> 
>> 
>> I'm teaching Python as the first programming language to non-computer
>> scientists. Many of the toy programs would be simpler with the double
>> comma syntax. Presently, having a choice whether to teach my students
>> the result += fn(x) way or your way, I would still opt for the former.
I guess you are assuming fn(x) returns a string, or result knows how to
do += for whatever you are passing to it?

BTW, what makes you think any self-respecting "scientist" wouldn't be insulted
by the idea of your spoon-feeding them a dumbed-down programming equivalent of
"See Spot run"? IMO, if you were coaching althletes like that, you'd be fired,
because none of your team except the naturals would be able to run the length
of the ball field, and the natural athletes would have no use or respect for you.

Of course, you may be a suffering victim of circumstances, I don't know. Fortunately,
Python is not that hard, and rapidly becomes fun unless the initial experience
is structured against discovering the expressive power that is so enjoyable.

If you want the effect of print x, y, fn(z), etc without spaces, why don't you
just write a function that will let you spell it simply, like

    marcinprint(x, y, fn(z), etc)

e.g.,
 >>> import sys
 >>> def marcinprint(*args):
 ...     sys.stdout.write(''.join(map(str, args)))
 ...
 >>> print 1,'two', 0x3, 4.5, fn(6)
 1 two 3 4.5
 Traceback (most recent call last):
   File "<stdin>", line 1, in ?
 NameError: name 'fn' is not defined

oops

 >>> def fn(x): return 2**x
 ...
 >>> print 1,'two', 0x3, 4.5, fn(6)
 1 two 3 4.5 64

 >>> marcinprint(1,'two', 0x3, 4.5, fn(6))
 1two34.564>>>

Might want to put a line ending on that ...

 >>> marcinprint(1,'two', 0x3, 4.5, fn(6), '\n')
 1two34.564
 >>> marcinprint(1,'two', 0x3, 4.5, fn(6)); print
 1two34.564

If you are used to C, you can get something familiar using

 >>> import sys
 >>> def printf(fmt, *args):
 ...     sys.stdout.write(fmt %args)
 ...

Which you can use like

 >>> printf('%03d %r %3d %5.2f %5d\n',1,'two', 0x3, 4.5, fn(6))
 001 'two'   3  4.50    64

>> 
>> Best regards,
>>   Marcin
>> 
>You could think about teaching them the linelist.append(fn(x)) way, 
>which then gives you the choice of
>
>   "".join(linelist) - no gaps
>   "\n".join(lienlist) - one item per line
>   " ".join(linelist) - spaces between items.
>
IMO teachers should lead their students into contact with reality,
not insulate them from it with delusionary simplifications that
they will have to unlearn in order to progress.

Regards,
Bengt Richter



More information about the Python-list mailing list