Where does str class represent its data?

ChrisEdgemon at gmail.com ChrisEdgemon at gmail.com
Wed Jul 11 19:21:22 EDT 2007


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

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

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