Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reversing support for iterators object. #36

Open
KmiQ opened this issue Oct 10, 2023 · 0 comments
Open

Reversing support for iterators object. #36

KmiQ opened this issue Oct 10, 2023 · 0 comments

Comments

@KmiQ
Copy link
Owner

KmiQ commented Oct 10, 2023

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.

@KmiQ KmiQ assigned KmiQ and unassigned KmiQ Jan 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant