I have a class that wraps around python deque from collections. When I go and create a deque x=deque(), and I want to reference the first variable....
In[78]: x[0]
Out[78]: 0
My question is how can use the [] for referencing in the following example wrapper
class deque_wrapper:
def __init__(self):
self.data_structure = deque()
def newCustomAddon(x):
return len(self.data_structure)
def __repr__(self):
return repr(self.data_structure)
Ie, continuing from above example:
In[75]: x[0]
Out[76]: TypeError: 'deque_wrapper' object does not support indexing
I want to customize my own referencing, is that possible?
deque, you should skim the Data model section of the docs (that you've now been given links to in both questions). Almost everything you're likely to ask for is going to be there.collections.abc.MutableSequence. If you inherit from that, and implement the give methods listed in the table, it'll automatically give you all the other methods thatdeque,list, and similar types have. (If you're in Python 2.x, it's calledcollections.MutableSequence, noabc… but if you're in 2.x, you shouldn't be declaring a class with no bases like this, as someone else explained on your previous question.)