On 30/06/15 07:47, Marilyn Davis wrote:
class MyList(list):
def __str__(self):
return """Here are your data:
%s
""" % list.__str__(self)
def main():
a = MyList([1,2])
print a
But if we add the special method:
def __repr__(self):
return "MyList(%s)" % (list.__str__(self))
we get:
File "./stack2.py", line 10, in __str__
""" % list.__str__(self)
File "./stack2.py", line 5, in __repr__
return "MyList(%s)" % (list.__str__(self))
File "./stack2.py", line 5, in __repr__
return "MyList(%s)" % (list.__str__(self))
RuntimeError: maximum recursion depth exceeded
If a class defines __repr__() but not __str__(), then __repr__() is also
used when an “informal” string representation of instances of that class
is required.
My guess is that list.__str__ is calling self.__repr__
But you have defined your own self.__repr__ so it gets called and
it then calls list.__str__ again, and again and ....
Try replacing the call to list.__str__ with a call to list.__repr__
and see if that fixes things?
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor