Where does str class represent its data?

ChrisEdgemon at gmail.com ChrisEdgemon at gmail.com
Thu Jul 12 01:21:22 CEST 2007

I'd like to implement a subclass of string that works like this:

>>>m = MyString('mail')
>>>m == 'fail'
>>>m == 'mail'
>>>m in ['fail', hail']

My best attempt for something like this is:

class MyString(str):
  def __init__(self, seq):
    if self == self.clean(seq): pass
    else: self = MyString(self.clean(seq))

  def clean(self, seq):
    seq = seq.replace("m", "f")

but this doesn't work.  Nothing gets changed.

I understand that I could just remove the clean function from the
class and call it every time, but I use this class in several
locations, and I think it would be much safer to have it do the
cleaning itself.

More information about the Python-list mailing list