"tostring" as a string method?
Andrew Dalke
dalke at acm.org
Sat Apr 1 01:48:15 EST 2000
Hello,
Strings in Python 1.6 will have methods. I would like
them to also have a "tostring()" method, which returns
itself.
I've been working on classes for storing and analyzing
protein and dna sequences. There are four ways I can
think of to store the data: a string, array.array,
Numeric.array, and a [] of single character strings.
Because of space requirements, [] is out, and Numeric.array
isn't available on all machines (and its length can't be
changed in-place).
Strings are more natural, because of the large number
of string operations, including regular expressions. An
array.array of characters is nice since it's mutable.
I would like to write functions which work on both strings
and array.arrays.
Here's an example, which is used to translate DNA to
protein:
def translate(dna, table = Translation.standard_table):
s = dna # or "dna.tostring()" for array.array
x = []
for i in range(0, len(s), 3):
x.append(table[s[i:i+3]])
return string.join(x, "")
This would work with both strings and arrays, if strings
supported a "tostring()" method. (I could give an example
with needing strings for regular expressions, but it would
be more complicated.)
Would there be any reason not to have tostring() as a method?
If not, I can supply a patch off the CVS version for it.
Andrew
dalke at acm.org
More information about the Python-list
mailing list