Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inherit and extend a list object in Python?

I am interested in using the python list object, but with slightly altered functionality. In particular, I would like the list to be 1-indexed instead of 0-indexed. E.g.:

>> mylist = MyList() >> mylist.extend([1,2,3,4,5]) >> print mylist[1] 

output should be: 1

But when I changed the __getitem__() and __setitem__() methods to do this, I was getting a RuntimeError: maximum recursion depth exceeded error. I tinkered around with these methods a lot but this is basically what I had in there:

class MyList(list):     def __getitem__(self, key):         return self[key-1]     def __setitem__(self, key, item):         self[key-1] = item 

I guess the problem is that self[key-1] is itself calling the same method it's defining. If so, how do I make it use the list() method instead of the MyList() method? I tried using super[key-1] instead of self[key-1] but that resulted in the complaint TypeError: 'type' object is unsubscriptable

Any ideas? Also if you could point me at a good tutorial for this that'd be great!

Thanks!

like image 494
mindthief Avatar asked Nov 04 '10 00:11

mindthief


People also ask

How do you inherit a list in Python?

To do this in Python, you can inherit from an abstract base class, subclass the built-in list class directly, or inherit from UserList , which lives in the collections module. In this tutorial, you'll learn how to: Create custom list-like classes by inheriting from the built-in list class.

How do you extend the length of a list in Python?

Python's append() function inserts a single element into an existing list. The element will be added to the end of the old list rather than being returned to a new list. Adds its argument as a single element to the end of a list. The length of the list increases by one.

How do you update an object in a list in Python?

You can update single or multiple elements of lists by giving the slice on the left-hand side of the assignment operator, and you can add to elements in a list with the append() method.

Can you use += for lists in Python?

extend(list2) adds the elements in list2 to the end of the list. Using + or += on a list is similar to using extend(). list. index(elem) -- searches for the given element from the start of the list and returns its index.


2 Answers

Use the super() function to call the method of the base class, or invoke the method directly:

class MyList(list):     def __getitem__(self, key):         return list.__getitem__(self, key-1) 

or

class MyList(list):     def __getitem__(self, key):         return super(MyList, self).__getitem__(key-1) 

However, this will not change the behavior of other list methods. For example, index remains unchanged, which can lead to unexpected results:

numbers = MyList() numbers.append("one") numbers.append("two")  print numbers.index('one') >>> 1  print numbers[numbers.index('one')] >>> 'two' 
like image 71
Gintautas Miliauskas Avatar answered Oct 02 '22 06:10

Gintautas Miliauskas


Instead, subclass integer using the same method to define all numbers to be minus one from what you set them to. Voila.

Sorry, I had to. It's like the joke about Microsoft defining dark as the standard.

like image 23
Binary Phile Avatar answered Oct 02 '22 05:10

Binary Phile