"tostring" as a string method?
Andrew Dalke
dalke at acm.org
Fri Apr 7 04:55:12 EDT 2000
Warren Postma said:
> Why not write str(dna) and overload __str__ where necessary?
> class myClass:
> def __init__(self,value='something'):
> self.value ='something'
> def __str__(self):
> return self.value
>
> myObject = myClass()
What I am looking for is a way to have the "value" be a string
or an array.array or even a Numeric.array. The solution you have
only works for strings. Adding a "tostring()" method to strings
would let me have a
def __str__(self): return self.value.tostring()
/F pointed out the buffer interface, which is the route I'm
going to look at - I'm only using strings now because it turns
out building up a [] and string.join'ing them is faster than
using an array.array. (Though I have ways to make arrays be
faster.)
Meanwhile, on the biopthon.org list, Jeff Chang pointed out that
__str__ is in general an ill-defined quantity. The usual
interpretation is that __str__ returns a human-representation while
__repr__ returns a computer representation. So using __str__
to return the character stream is not the right interface. We're
now using "tostring()", which is more obvious.
Andrew
dalke at acm.org
More information about the Python-list
mailing list