You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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.
The text was updated successfully, but these errors were encountered: