"tostring" as a string method?

Warren Postma wpostma at earthlink.net
Thu Apr 6 19:51:07 EDT 2000


>   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, "")
>

Why not write str(dna) and overload __str__ where necessary?


# polymorphism exhibited for all types, conversion to string:

class myClass:
 def __init__(self,value='something'):
  self.value ='something'
 def __str__(self):
  return self.value
myObject = myClass()

a = [ 5, 5.5, myObject, 'test']

for i in a:
 print str(i)


Warren





More information about the Python-list mailing list