reversed()Advanced Examples

Returns a reverse iterator over a sequence

reversed() protocol implementation

Implementing the protocol that reversed() uses under the hood.

python
# reversed() - implementing the protocol
class Custom:
    def __reversed__(self):
        return "custom result"

obj = Custom()
print(reversed(obj))

Understanding the dunder methods that reversed() calls helps you customize behavior for your own classes.

Edge cases with reversed()

Handling unusual inputs and edge cases.

python
# reversed() edge cases
print("Edge case handling for reversed()")

# Type checking
print(type(42))

Knowing these edge cases prevents subtle bugs in production.

Want to try these examples interactively?

Open Advanced Playground