0

I'm inheriting from the python list class but I can't seem to figure out how to modify the list itself like below:

class MyList(list):
    def __init__(self):
        super().__init__()
        
        self = [0, 0, 0, 0]

a = MyList()
print(a) # [] instead of [0, 0, 0, 0]
4
  • Override __str__? Commented Apr 27, 2021 at 8:05
  • @AbdulNiyasPM No, I would like to change the list itslef not only how it shows when you print Commented Apr 27, 2021 at 8:07
  • Subclassing list is a little bit tricky. Maybe you could subclass collections.UserList instead? Commented Apr 27, 2021 at 8:09
  • 1
    self.extend([0, 0, 0, 0]) in the init Commented Apr 27, 2021 at 8:10

1 Answer 1

1

You need to pass the list in your parent's class initializer super().__init__([0, 0, 0, 0]). and you should notice that subclassing built-in collections may result in odd behaviors. Check this link for more information.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.