[Tutor] Subclassing

Eddie Comber python at comber.cix.co.uk
Mon Dec 29 12:54:13 EST 2003


Yes but look at:

f = d[1:2]

print f, type(f)

The type of f is <list> and this would require me to  subclass, or overload,
the slice operator to give me the correct return type (and every other
operator valid for a <list> type)

class mylist(list):
     def __init__(self, inList=None):
         if inList:
             self[:] = inList
     def __add__(self, other):
         self.extend(other)
         return mylist(self)
a = mylist([1])
b = mylist([2])
c = mylist([3])

d = a + b + c

f = d[1:2]

print f, type(f)

-----Original Message-----
From: Bob Gailer [mailto:bgailer at alum.rpi.edu]
Sent: 22 December 2003 18:10
To: Eddie Comber; tutor at python.org
Subject: Re: [Tutor] Subclassing


At 01:27 AM 12/22/2003, Eddie Comber wrote:

>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.

Try this:

class mylist(list):
     def __init__(self, inList=None):
         if inList:
             self[:] = inList
     def __add__(self, other):
         self.extend(other)
         return mylist(self)
a = mylist([1])
b = mylist([2])
c = mylist([3])

d = a + b + c
print d # displays [1, 2, 3]

x = 3
i = 1
j = 2
n = 3
t = [5, 6]
print x in s        # 1 if an item of s is equal to x, else 0
print x not in s    # 0 if an item of s is equal to x, else 1
print s + t         # the concatenation of s and t
print s * n , n * s #   n shallow copies of s concatenated      (1)
print s[i]          # i'th item of s, origin 0  (2)
print s[i:j]        #   slice of s from i to j  (2), (3)
print len(s)        #   length of s
print min(s)        #   smallest item of s
print max(s)

s[i] = x       ; print s # item i of s is replaced by x
s[i:j] = t     ; print s # slice of s from i to j is replaced by t
del s[i:j]     ; print s # same as s[i:j] = []
s.append(x)    ; print s # same as s[len(s):len(s)] = [x]       (1)
s.extend([x])  ; print s # same as s[len(s):len(s)] = x         (2)
s.count(x)     ; print s # return number of i's for which s[i] == x
s.index(x)         ; print s # return smallest i such that s[i] == x    (3)
s.insert(i, x) ; print s # same as s[i:i] = [x] if i >= 0       (4)
s.pop(i)       ; print s #same as x = s[i]; del s[i]; return x  (5)
s.remove(x)    ; print s #same as del s[s.index(x)]     (3)
s.reverse()    ; print s #reverses the items of s in place      (6)
s.sort()           ; print s #sort the items of s in place

Bob Gailer
bgailer at alum.rpi.edu
303 442 2625 home
720 938 2625 cell




More information about the Tutor mailing list