Strange __str__ behavior

Emile van Sebille emile at fenx.com
Fri Nov 9 15:13:11 EST 2001


---------
<brucehapman at hotmail.com> wrote in message
news:baf2f841.0111091141.7a104b10 at posting.google.com...
> I am defining a class w/ method __str__. If I put a print statement
> inside __str__, and if I then use the print statement to display an
> instance of the class, I get an extra space in the output.
>
> <example>
>
> Python 2.2b1 (#25, Oct 19 2001, 11:44:52) [MSC 32 bit (Intel)] on
> win32
> Type "help", "copyright", "credits" or "license" for more information.
> >>> class Test:
> ...     def __str__(self):
> ...         print "In method Test.__str__"
> ...         return "String representation of instance."
> ...
> >>> test = Test()
> >>> print test       # causes extra space problem
>  In method Test.__str__
> String representation of instance.
>
> </example>
>


I don't get the same thing on win2k

F:\Python22>python
ActivePython 2.2 Alpha 2 build 1 (ActiveState)
Python 2.2a2+ (#22, Sep  5 2001, 14:10:41) [MSC 32 bit (Intel)] on win32
>>> class Test:
...     def __str__(self):
...             print "in __str__"
...             return "str(self)"
...
>>> test = Test()
>>> print test
 in __str__
str(self)
>>>


I do have other flakey additional/missing line space problems when not
directly running from the dos command prompt, more with win2k it seems that
with win9x.

But I don't know where the problem is.  ;-)


ps.  ms outlook express spell-check wants to correct win2k to <wink>  ;-))

--

Emile van Sebille
emile at fenx.com




More information about the Python-list mailing list