Re:Python Programming for the Privileged Class
by jmpeax on Sunday June 01, 2014 @05:17AM
(#7828620)
The idea would be, in this case, to create a method called "toHtmlString". The name "toString" suggests a generic String is created for the object, but HtmlString represents a specially formatted String.
Rather than having a class implement HtmlString as a way to specify that its "toString" method works differently to the usual or expected way for a method of that signature, you can instead write a "toHtmlString" which is more clear. Reading code as "myObject.toString()" vs "myObject.toHtmlString()" is better than having to check the class signature for a special contract pertaining to general-looking method names.
Rather than having a class implement HtmlString as a way to specify that its "toString" method works differently to the usual or expected way for a method of that signature, you can instead write a "toHtmlString" which is more clear. Reading code as "myObject.toString()" vs "myObject.toHtmlString()" is better than having to check the class signature for a special contract pertaining to general-looking method names.