On 21/01/14 22:21, Will Fitch wrote:
> Because the __toString allows a class to decide how to react when
> treated like a string. Sure, that could be debug info, but it could be
> printing a full-blow decorator implementation (e.g. Zend\Form\Form), a
> number, class name, etc. What Sara is proposing is specific to object
> debugging.
Right. To see the difference better, I suggest looking at what Python
does. It has a __string__() magic method and a __repr__() magic method,
for string conversion and debug info, respectively. For a date, these
result in:
>>> x = datetime.datetime(1996, 02, 17)
>>> x
datetime.datetime(1996, 2, 17, 0, 0)
>>> str(x)
'1996-02-17 00:00:00'
>>> repr(x)
'datetime.datetime(1996, 2, 17, 0, 0)'
While the str() result is what you want for displaying that date, the
repr() result shows you how it was constructed, which is more useful for
debugging.
For another example, see what Python strings do here:
>>> n = "line 1\nline 2"
>>> print n
line 1
line 2
>>> print repr(n)
'line 1\nline 2'
The first one's might not be very useful for debugging, because it
outputs control characters raw. The debug output version, however, is
much better for this.
--
Andrea Faulds
http://ajf.me/