print without newline?
Fredrik Lundh
effbot at telia.com
Sat Sep 2 09:21:21 EDT 2000
ian wrote:
> >Actually, it doesn't put a space after the "Hello.", the next print
> >statement puts a space before its output.
>
> I'm sure there is logic behind this choice, but it escapes me.
>
> Can anyone explain why print should put a space out first?
here's how it works:
a print statement is compiled into several bytecode instructions
(opcodes); there's one PRINT_ITEM for each comma-separated
item, and one PRINT_NEWLINE at the end (unless the statement
ends with a comma).
:::
in other words,
print a, b, c
is compiled into:
PRINT_ITEM a
PRINT_ITEM b
PRINT_ITEM c
PRINT_NEWLINE
while
print a, b, c,
results in:
PRINT_ITEM a
PRINT_ITEM b
PRINT_ITEM c
each file-like object implements a "softspace" attribute, which
is used by these two opcodes. if this flag is set, PRINT_ITEM
prints a space before the actual item.
PRINT_ITEM always sets the flag, PRINT_NEWLINE clears it:
>>> print "a", ; sys.stdout.softspace = 0 ; print "b"
ab
>>> print "a" ; sys.stdout.softspace = 1 ; print "b"
a
b
>>>
> And can they confirm that the FIRST print in the program does not.
don't you think we would have noticed by now? ;-)
</F>
<!-- (the eff-bot guide to) the standard python library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->
More information about the Python-list
mailing list