range()Advanced Examples

Generates an immutable sequence of integers

range() protocol implementation

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

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

obj = Custom()
print(range(obj))

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

Edge cases with range()

Handling unusual inputs and edge cases.

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

# Type checking
print(type(42))

Knowing these edge cases prevents subtle bugs in production.

Want to try these examples interactively?

Open Advanced Playground