简体   繁体   中英

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM