inheritance, types, operator overload, head ache

thorley at gmail.com thorley at gmail.com
Tue Jul 11 14:57:25 EDT 2006


> > > Can some one *please* splain me why str(obj) works but not print obj,
> >
> > May have something to do with escape chars... I tried with:
> >    def __str__(self):
> >       return repr(self)

Yes, that appears to be correct. Experiments indicated that it's an
issue with escaping.


> > What you want here is to first create the instance, and only then bind
> > to it:
> >
> >      def __new__(cls, val):
> >          if type(val) == str and not val.isdigit():
> >              val = struct.unpack('B', val)
> >          _byte = struct.pack('B', val)
> >          self = str.__new__(cls, _byte)
> >          self._byte = _byte
> >          self._int = int(val)
> >          return self

Oh, I see. I tried that and it worked well, but the broken int(obj) is
too annoying. I've decided to just implement the needed str methods
myself, and inherit from object. This means join will be broken, so
I'll have to do ''.join(map(str, (b0, b1))), which, for my purposes is
probably the best compromise.

Thanks again
--
matthew




More information about the Python-list mailing list