[Tutor] Subclassing

Eddie Comber python at comber.cix.co.uk
Mon Dec 22 03:27:15 EST 2003


Suppose I want to subclass 'list' to provide mylist which has different
behaviour. Say I only want to modify the '+' operator __add(self, other)__
as the other operators and functions are fine.

The problem is what type should '+' return?

If it returns type == list then

a = mylist()
b = mylist()
c = mylist()

d = a + b + c

means after b + c the type is 'list' and a + b is (mylist + list) rather
than (mylist + mylist).

The trouble with returning type mylist is that it looks to me like all the
'list' operators and functions need to be trivially subclassed to return
type mylist.

Am I right in this?

ta,
Eddie,





More information about the Tutor mailing list