print without newline?
effbot at telia.com
Sat Sep 2 15:21:21 CEST 2000
> >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 a, b, 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"
>>> print "a" ; sys.stdout.softspace = 1 ; print "b"
> And can they confirm that the FIRST print in the program does not.
don't you think we would have noticed by now? ;-)
<!-- (the eff-bot guide to) the standard python library:
More information about the Python-list