can I call operator overloader in superclass?

Jon J. Morin rotundo52 at hotmail.com
Thu Jun 20 00:42:15 EDT 2002


Hi all.  I have a particularly trivial bit of code I'm working with to learn 
Python and I have a question about operator overloading.  Say I have class 
AList which is partially defined:

class AList:
        def __init__(self, other=[]):
                self.data = []
                for x in other:
                        self.data.append(x)
        def __repr__(self):
                return "List: " + `self.data`
...
...
...

I have a subclass AListSub which is partially defined as follows:

classAListSub(AList):
        repr_calls = 0
        def __init__(self, list=[]):
                AList.__init__(self, list)
        def __repr__(self):
                AListSub.repr_calls = AListSub.repr_calls + 1
                print "Calling __repr__ in superclass"
                AList.__repr__(self)
                print "Method called %d times." % (AListSub.repr_calls)
...
...
...
I can create instances on AListSub by passing in a string, or a list as in
>>> x = AListSub('sasquatch')
>>> x
Calling __repr__ in superclass
Method called 1 times.
Traceback (innermost last):
  File "<stdin>", line 1, in ?
TypeError: repr not string

For one, I don't understand the error.  For seconds, can I call __repr__ in 
the superclass from __repr__ in the subclass?

Jon J. Morin



More information about the Python-list mailing list