473,411 Members | 1,880 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

sys.stdout question

>>> import sys
class stuff: .... things = []
.... def write(self, string):
.... self.things.append(string)
.... def_stdout = sys.stdout
sys.stdout = stuff()
print 'this is a string.'
print 'This is another string.'
sys.stdout = def_stdout
print stuff.things

['This is a string.', '\n', 'This is another string.', '\n']

Where are the newline characters coming from?

Jul 19 '05 #1
5 3293
Print is the culprit here. Note item 2.
help('print')

6.6 The print statement

print_stmt ::= "print" ( [expression[1] ("," expression[2])*
[","]]
| ">>" expression[3] [("," expression[4])+ [","]] )

Download entire grammar as text.[5]

print evaluates each expression in turn and writes the resulting
object
to standard output (see below). If an object is not a string, it is
first converted to a string using the rules for string conversions.
The
(resulting or original) string is then written. A space is written
before each object is (converted and) written, unless the output
system
believes it is positioned at the beginning of a line. This is the
case
(1) when no characters have yet been written to standard output, (2)
when the last character written to standard output is "\n", or (3)
when
the last write operation on standard output was not a print
statement.
(In some cases it may be functional to write an empty string to
standard
output for this reason.) Note: Objects which act like file objects
but
which are not the built-in file objects often do not properly emulate
this aspect of the file object's behavior, so it is best not to rely
on
this.

A "\n" character is written at the end, unless the print statement
ends
with a comma. This is the only action if the statement contains just
the
keyword print.

Standard output is defined as the file object named stdout in the
built-in module sys. If no such object exists, or if it does not have
a
write() method, a RuntimeError exception is raised.

print also has an extended form, defined by the second portion of the
syntax described above. This form is sometimes referred to as ``print
chevron.'' In this form, the first expression after the >> must
evaluate
to a ``file-like'' object, specifically an object that has a write()
method as described above. With this extended form, the subsequent
expressions are printed to this file object. If the first expression
evaluates to None, then sys.stdout is used as the file for output.

hth,
M.E.Farmer

Jul 19 '05 #2
chris patton wrote:
import sys
class stuff:
... things = []
... def write(self, string):
... self.things.append(string)
...
def_stdout = sys.stdout
sys.stdout = stuff()
print 'this is a string.'
print 'This is another string.'
sys.stdout = def_stdout
print stuff.things


['This is a string.', '\n', 'This is another string.', '\n']

Where are the newline characters coming from?

The print statement automatically inserts a newline at the end of its
output unless the statement ends in a comma. It also puts a space
between the comma-separated items.

regards
Steve
--
Steve Holden +1 703 861 4237 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/

Jul 19 '05 #3
I tried adding the comma at the end
print 'hello',
It still added that extra character.

Jul 19 '05 #4
Save your script and run that from a commandline or import it with your
python shell.
['this is a string.', ' ', 'This is another string.']
You are seeing a side effect of the interactive shell.
['this is a string.', '\n', 'This is another string.','\n']
It would be hard to get to the next prompt after a print "dsfdfdssd",
if the shell didn't add the \n to get it there.
Also note most shells use repr or str behind the scenes for output to
sys.stdout sys.stderr.
def g(): .... pass
.... g # notice it has been repr'd for you <function g at 0x0077AB88> repr(g)

'<function g at 0x0077AB88>'
M.E.Farmer

Jul 19 '05 #5
chris patton wrote:
I tried adding the comma at the end
print 'hello',
It still added that extra character.


http://mail.python.org/pipermail/pyt...er/184181.html

Peter

--
In diesen Kreisen kreiselt sich der Kreisler

Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Gian Mario Tagliaretti | last post by:
Hi all, It is (I hope) a simple question, I cannot figure out why sys.stdout.write() doesn't print immediatly the first text in the small example below, but before it process the code in between...
4
by: Leon | last post by:
"stdout" is file object, it open mode is "w" and it have a close() fuction..... while it run close(), how to reopen it because I want to do rewrite it stdout content update
3
by: David Douard | last post by:
Hi everybody, let me explain by problem: I am working on an application which consists in a C++ dll (numeric computations) and a Python IHM (Python/Tk), which must run under Linux and win32. My...
0
by: bwaha | last post by:
I've posted this question to comp.graphics.apps.gnuplot too but given that this python group may also comprise a lot of gnuplot users, and is far more active, I've posted this question here too. My...
7
by: Andre | last post by:
Hi, I have a program that sends some output to stdout and some to stderr. I need to separate the two using the command-line so that I direct stderr output to a file, say fileA.txt, and stdout...
7
by: Mathias Herrmann | last post by:
Hi. I have the following problem: Using popen() to execute a program and read its stdout works usually fine. Now I try to do this with a program called xsupplicant (maybe one knows), but I dont...
9
by: Santtu Nyrhinen | last post by:
Hi, Let say that I have a function like void writeHello() { printf("Hello"); } Now I need to make an automated test fot that function. The test function returns 1 for successful and 0 for...
3
by: Fuzzyman | last post by:
Hello all, Before I ask the question a couple of notes : * This question is for implementing a script inside the Wing IDE. For some reason using the subprocess module doesn't work so I need a...
4
by: hg | last post by:
Hi, I have the following ********************* C extention - redir.c #include "Python.h" PyObject * test_redir_test(PyObject *self) {
5
by: Joakim Hove | last post by:
Hello, I have written a program in C; this programs uses an external proprietary library. When calling a certain function in the external library, the particular function writes a message to...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.