Skip to content

Reversing support for iterators object. #36

@KmiQ

Description

@KmiQ

The error "'object is not reversible" arises when the reversed() function is called on an object that doesn't define a reversed() method and doesn't have a len() method in combination with getitem(). In simpler terms, Python doesn't know how to reverse iterate over the object.

In the case of your CandlesPage object, you've provided an iterator using the iter() method, which allows for forward iteration. To enable reverse iteration, you need to implement the reversed() method.

Here's how you can add the reversed() method to your CandlesPage class:

python
Copy code
def reversed(self):
return reversed(self.candles)
By adding this method, you should be able to reverse iterate over an instance of CandlesPage using the reversed() function:

python
Copy code
page = CandlesPage([...]) # some list of candles
for candle in reversed(page):
# do something
Another thing to note is that it's generally a good idea to check if self.candles is None in both iter() and reversed(). You're currently setting self.candles to None if the input is None, and this would raise an exception if someone tries to iterate over it in that state.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions