Re: sys.stdout question

2005-05-02 Thread Peter Otten
chris patton wrote:

> I tried adding the comma at the end
> print 'hello',
> It still added that extra character.

http://mail.python.org/pipermail/python-list/2003-September/184181.html

Peter

-- 
In diesen Kreisen kreiselt sich der Kreisler

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sys.stdout question

2005-05-02 Thread M.E.Farmer
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

>>> repr(g)
''
M.E.Farmer

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sys.stdout question

2005-05-02 Thread chris patton
I tried adding the comma at the end
print 'hello', 
It still added that extra character.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sys.stdout question

2005-05-01 Thread M.E.Farmer
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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sys.stdout question

2005-05-01 Thread Steve Holden
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/

-- 
http://mail.python.org/mailman/listinfo/python-list