OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to subclass Python list without type problems?

  • Thread starter Thread starter silvado
  • Start date Start date
S

silvado

Guest
I want to implement a custom list class in Python as a subclass of list. What is the minimal set of methods I need to override from the base list class in order to get full type compatibility for all list operations?

This question suggest that at least __getslice__ needs to be overridden. From further research, also __add__ and __mul__ will be required. So I have this code:

Code:
class CustomList(list):
    def __getslice__(self,i,j):
        return CustomList(list.__getslice__(self, i, j))
    def __add__(self,other):
        return CustomList(list.__add__(self,other))
    def __mul__(self,other):
        return CustomList(list.__mul__(self,other))

The following statements work as desired, even without the overriding methods:

Code:
l = CustomList((1,2,3))
l.append(4)                       
l[0] = -1
l[0:2] = CustomList((10,11))    # type(l) is CustomList

These statements work only with the overriding methods in the above class definition:

Code:
l3 = l + CustomList((4,5,6))    # type(l3) is CustomList
l4 = 3*l                        # type(l4) is CustomList
l5 = l[0:2]                     # type(l5) is CustomList

The only thing I don't know how to achieve is making extended slicing return the right type:

Code:
l6 = l[0:2:2]                   # type(l6) is list

What do I need to add to my class definition in order to get CustomList as type of l6?

Also, are there other list operations other than extended slicing, where the result will be of list type instead of CustomList?
<p>I want to implement a custom list class in Python as a subclass of <code>list</code>. What is the minimal set of methods I need to override from the base <code>list</code> class in order to get full type compatibility for all list operations?</p>

<p><a href="https://stackoverflow.com/questions/2235556/python-subclass-builtin-list">This question</a> suggest that at least <code>__getslice__</code> needs to be overridden. From further research, also <code>__add__</code> and <code>__mul__</code> will be required. So I have this code:</p>

<pre><code>class CustomList(list):
def __getslice__(self,i,j):
return CustomList(list.__getslice__(self, i, j))
def __add__(self,other):
return CustomList(list.__add__(self,other))
def __mul__(self,other):
return CustomList(list.__mul__(self,other))
</code></pre>

<p>The following statements work as desired, even without the overriding methods:</p>

<pre><code>l = CustomList((1,2,3))
l.append(4)
l[0] = -1
l[0:2] = CustomList((10,11)) # type(l) is CustomList
</code></pre>

<p>These statements work only with the overriding methods in the above class definition:</p>

<pre><code>l3 = l + CustomList((4,5,6)) # type(l3) is CustomList
l4 = 3*l # type(l4) is CustomList
l5 = l[0:2] # type(l5) is CustomList
</code></pre>

<p>The only thing I don't know how to achieve is making extended slicing return the right type:</p>

<pre><code>l6 = l[0:2:2] # type(l6) is list
</code></pre>

<p>What do I need to add to my class definition in order to get <code>CustomList</code> as type of <code>l6</code>?</p>

<p>Also, are there other list operations other than extended slicing, where the result will be of <code>list</code> type instead of <code>CustomList</code>?</p>
 

Latest posts

Top