how to use __str__ and __repr__?

Larry Bates lbates at swamisoft.com
Tue Jun 8 00:38:49 CEST 2004


I don't understand what you are trying to do, but
the problem is that when you define class 'another'
the following line doesn't make sense:

middle = " ".join( [ substr.__str__() for substr in self])

The instance of another doesn't have a __str__ method
defined (e.g. it's an empty class).  All of your other
tests have a class that does have a __str__ method
because it was inherited from the baseclass list.

You could try:

class another(list):
    pass

Larry Bates
Syscon, Inc.

"Jim Newton" <jimka at rdrop.com> wrote in message
news:2ik7qrFo8httU1 at uni-berlin.de...
> hi all, does anyone know what print does if there is no __str__ method?
> i'm trying ot override the __repr__.   If anyone can give me some advice
> it would be great to have.
>
> I have defined a lisp-like linked list class as a subclass of list.
> The __iter__ seems to work as i'd like, by traversing the links,
> and the __repr__ seems to work properly for somethings but not others.
>
> The basic idea is that a list such as [ 1, 2, 3, 4] is converted to
> [1, [2, [3, [4, nil]]], thus allowing me to push (cons) a new element
> non-destructively onto the beginning of the list; e.g.
>
> x = seq2pair( [ 3,4,5]) --> [ 3, [4, [5, nil]]]
> y = x.cons(2)           --> [ 2, [3, [4, [5, nil]]]
> z = y.cons(1)           --> [ 1, [ 2, [3, [4, [5, nil]]]]
>
> for elt in z: # iterates elt=1, elt=2, elt=3 ...
>     pass
>
> I would love to know how to define the __repr__ or __str__
> method so that it is able to print everything the way print
> normally works, except that instances of my class gets printed
> special. I want to print [ 1, [ 2, [3, [4, [5, nil]]]]
> simple as a space seperated list.  It works most of the time.
> --> ( 1 2 3 4 5)
>
> Another question is whether there is a way to promote an
> empty list [] to an instance of Pair?
>
> class Pair(list):
>
>      def __iter__(self):
>          while self:
>              yield self.car()
>              self = self.cdr()
>
>      def __repr__(self):
>          middle = " ".join( [ substr.__str__() for substr in self])
>          return "( " + middle + " )"
>
>      #  x = (cons x l_y)
>      # ==> x = l_y.cons(x)
>      def cons(self, car):
>          new = Pair()
>          new.append(car)
>          new.append(self)
>          return new
>
>      def car(self):
>          if self:
>              return self[0]
>          return nil
>
>      def cdr(self):
>          if len(self)<2:
>              return nil
>          return self[1]
>
> nil = Pair()
>
>
> # [ 1, 2, 3] --> [1, [2, [3, nil]]]
> def seq2pair(seq):
>      new = Pair()
>      for index in xrange( len(seq), 0, -1):
>          new = new.cons(seq[index - 1])
>      return new
>
> mylist = seq2pair( [1,2,3,4,5])
> print mylist   #  correctly prints --> ( 1 2 3 4 5)
>
>
> mylist2 = seq2pair( [11.1, 21.1, 31.1, 41.1, mylist])
> print mylist2
> #  correctly prints --> ( 11.1 21.1 31.1 41.1 ( 1 2 3 4 5 ) )
>
> class another:
>      pass
>
> print another()  # prints --> <__main__.another instance at 0x8132b64>
>
> # ????????????????????????????????????????
> print seq2pair( [ another(), another()  ]) # FAILS
> # ????????????????????????????????????????
>
> Traceback (most recent call last):
>    File "xyz.py", line 52, in ?
>      print seq2pair( [ another(), another()  ])
>    File "xyz.py", line 13, in __repr__
>      return "( " + " ".join( [ substr.__str__() for substr in self]) +
" )"
> AttributeError: another instance has no attribute '__str__'
>





More information about the Python-list mailing list