0

So lets say I have a class Movies that is iterable and self.movies is a list of Movie objects. How can I create remove_movie method? Method I provided doesn't work because iterable isn't list?

class Movies:
    def __init__(self):
        self.movies = []

    def __iter__(self):
        return iter(self.movies)

    def add_movie(self, movie):
        pass

    def remove_movie(self, id):
        for movie in self:
            if movie.id == id:
               self.remove(movie)
3
  • 1
    self.movies.remove? But beware changing a list's length while iterating over it.
    – jonrsharpe
    Mar 18, 2020 at 22:26
  • Surely you have access to the underlying list: self.movies.remove(movie); return
    – quamrana
    Mar 18, 2020 at 22:26
  • jonrsharpe, quamrana yes you are right, i totaly overlooked it. sorry Mar 18, 2020 at 22:30

1 Answer 1

2

You are getting an AttributeError.

self.movies.remove(movie)
1
  • that is correct, but is there a way to actualy do it on self without accessing atribute? like some dunder method for iterables, or it is how it should be done? Mar 18, 2020 at 22:34

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.