Daffy
Daffy

Reputation: 851

When making a class inherit `list` in Python, how can I get an instance of that list?

I'm trying to write a heap structure, and I realized most of what I was implementing in terms of indexing and setting items was already implemented in the form of list. How do I get an instance of that list so I can use it for the heap building?

This is about as far as I got

class heap(list):
    def __init__(self, arr=None, key=None):
        super().__init__(arr)
        self._arr = arr

I want self._arr to be the instance of the list created on the line directly above.

Upvotes: 2

Views: 80

Answers (1)

lxop
lxop

Reputation: 8595

Your heap doesn't have a list, it is a list. As such, you don't access the list through an instance attribute, the instance is the list.

Depending on what you want to do with the list, you should operate directly on self.

Upvotes: 5

Related Questions