Create an Iterator | python iterators

Опубликовано: 16 Ноябрь 2022
на канале: Coding Study Point
112
like

Create an Iterator
To create an object/class as an iterator you have to implement the methods __iter__() and __next__() to your object.

As you have learned in the Python Classes/Objects chapter, all classes have a function called __init__(), which allows you to do some initializing when the object is being created.

The __iter__() method acts similar, you can do operations (initializing etc.), but must always return the iterator object itself.

The __next__() method also allows you to do operations, and must return the next item in the sequence.

Example
Create an iterator that returns numbers, starting with 1, and each sequence will increase by one (returning 1,2,3,4,5 etc.):


На этой странице сайта вы можете посмотреть видео онлайн Create an Iterator | python iterators длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь Coding Study Point 16 Ноябрь 2022, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 112 раз и оно понравилось like зрителям. Приятного просмотра!