[Python] How to make a custom class work with the ‘for in’ loop in Python?

Discussão em 'Python' iniciado por Stack, Março 9, 2025.

  1. Stack

    Stack Membro Participativo

    If I have a custom class, how can I make it work with a ‘for in’ loop, like this:

    class number_range:
    def __init__(self, start, end):
    self.__data = []
    for i in range(start, end):
    self.__data.append(i)
    #something that will allow the for in loop

    numbers = number_range(1, 100)
    for number in numbers:
    print(number)

    Continue reading...

Compartilhe esta Página