Strange __str__ behavior

Steve Holden sholden at holdenweb.com
Fri Nov 9 16:29:47 EST 2001


"Emile van Sebille" <emile at fenx.com> wrote ...

> <brucehapman at hotmail.com> wrote ...
> > 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__
  ^
Yes, you do!

> str(self)
> >>>
[ ... ]
regards
 Steve
-- 
http://www.holdenweb.com/








More information about the Python-list mailing list