Level 10 : What happens when we execute this iterator code?


class StepIterator:
    def __init__(self, start, step, end):
        self.current = start
        self.step = step
        self.end = end

    def __iter__(self):
        return self

    def __next__(self):
        if self.current >= self.end:
            raise StopIteration
        value = self.current
        self.current += self.step
        return value

it = StepIterator(2, 3, 10)
print(list(it))

Author: SandhyaR | Tags: iterator | 1859+ Python FAQs | Share
__all__-attribute | arithmetic-operation | built-in | boolean-and-non-boolean | built-in-function | data-structure | context-manager | interpreter | floats | data-structures | default-mutable-argument | file | design-principle | compute | intering | def | heap-memory | generators-in-python | functions | indexing:-loc-vs-iloc | error-handling | complex | implementation | dictionaries | difference-between-threads-and-processes | higher-order-function | destructor | formal-and-actual-parameter | global-variable | lambdas | defining-functions | creating-and-starting-threads | copy | exception | expand-tab | gil | id-function | library | control-flow | filtering-and-indexing | condition | generator | join | list | input-function | async | frozenset | factorial-function | conversion | global-keyword | iterators | functional-programming | input-function | format | directory-operations | file-handling | encoding | function | dataframe-and-series-objects | data-type-conversions | error-handling | else-statement | command-line-parsing | file_handling | attribute | list-comprehensions | inherit | datetime | function-and-scope | conditional-statement | data-types | control-statement | else | application | __future__ | integers | indexing | assert | integer | default-arguments | dictionary | conditional-statements | filter | bitwise-operator | fabs-function | arbitrary-arguments-(*args,-**kwargs) | binary | exception-handling | bitwise-operator | call | dictionary-methods | asyncio-module | enumerate() | feature | docstring | __init_subclass__ | immutability | identity-operators-(is,-is-not) | access-modifier | arithmetic-expression | booleans | invalid-list | array | input | advanced-file-operations | basic | data-type | int | descriptors | exception-handling | data-type | aggregating-data-with-functools.reduce() | lambda-functions | data-structures-in-python | indentation | expression | filter-function | asyncio | decorator | collection | escape-operator | dicts | list-indexing | goto-function | bitwise | exponentiation | encapsulation | lambda-function | counter-function | iterator | anchors-(^,-$) | functional-programming | floating-point-precision | class-and-inheritance | error | built-in-modules | classes | file-handling | function-argument | deep-copy | benefit | counter | built-in-function | data-structure | lambda | head-and-tail | append | constructor | formatting-with-strftime | identity-operator | creating-custom-modules | datatype | center | global | class-decorators | handling-missing-values-(fillna,-dropna) | assignment-operators | generators | dictionary-comprehensions | global-variable | list-join | identifier | boolean | key-value-pairs | assignment-operator | choice | identity-operator | immutable-data-type | list-comprehension | init | joining-and-merging-dataframes | complex-number | iterable | collections | introduction-to-threads | class | boolean-masking | introduction-to-multiprocessing | binary-operation | class-and-object | comment | inheritance | import-method | installation | introduction-to-python | keyword | comparison | for | class-and-instance-variable | datetime.date,-datetime.time,-datetime.datetime | thread-safe-data-structures-(queue.queue) | debugging | pandas | namespace | control-flow | decimal | data_structure | loops | quantifiers-(*,-+,-?,-{}) | comprehension | sorted | numpy | sum-function | string-iteration | nested-loop | list-methods-and-slicing | version | syntax-error | yield | multiple-except-blocks | oops | multiple-inheritance | map-function | python | tuple-methods | working-with-file-paths | min-function | stack | string | logical-operators | parsing-with-strptime | turtle | strings | rollback | shallow-copy | using-assertions-(assertequal,-assertraises) | thread | syntax | threading | list-slicing | pickling--unpickling | time-series-with-pandas | string-formatting | print | list-operation | string-methods | managing-directories-with-os-and-shutil | set-operation | membership-operators-(in,-not-in) | syntaxerror | working-with-temporary-files-using-tempfile | logical-operation | methods | while-and-for-loop | string-manipulation | using-the-threading-module | string-method | using-map()-for-transformations | raise-statement | working-with-binary-files-(rb,-wb) | multiprocessing | using-else-and-finally | round-function | overriding-methods | unit-testing | object-attribute | regex | python-module | virtual-environment | list-method | matplotlib | working-with-dates-and-times | swapping | test-discovery | sys-module | writing:-write(),-writelines() | mapping-function | sum() | python-history | string-method | string-slicing | slice | mysql | operator | random | walking-through-directories-using-os.walk() | type-conversion | reading-and-writing-large-files-efficiently | while-loop | loop-control | objects | recurssion | operators | operator-or | split | reading-and-writing-files | postfix | sys | list-comprehension | update | list-vs-tuple | namedtuple | regular-expressions | random-function | numeric | overflow | type-conversion-and-casting | single-inheritance | operations | type-conversion | odd-number | regular-expression | zfill-function | oop | python_version | using-lists-as-stacks | method | operand | return-statement | min-max-function | using-os-and-pathlib-modules | zip-function | sets | using-yield-in-functions | python-repl | math | string-slicing | pep | statement | loop-control-statements | python-operators | opening-files-(open()) | with-statement | math-copysign | literal-characters-and-special-characters | operator-and | name-mangling | pseudocode | python-syntax-and-structure | mathematics | package-management | precedence-and-associativity | set | parameter | match | slicing | thread-synchronization | python-django | typeerror | matrix-list-comprehension | round | recursion | matrix | method-overloading | virtual-environment | tuples | polymorphism | scientific-notation | multithreading | logical-operator | software-design | while-loops | string-formatting | multiple-inheritance | operator-overloading | memory | why-use-multiprocessing-over-threading?-(gil) | precedence | pass | string-concatenation | mathematical-function | zip | range | static-method | tuple-unpacking | type_conversion | touples | raise | using-join()-and-is_alive()-methods | print-statement | staticmethod | list,-set,-and-dictionary-comprehensions | lists | memory-management | reading:-read(),-readline(),-readlines() | syntax-error | max-min | list-mutation | sorting | sorting-and-ranking | version-check | magic-method | while-else-loop | tuple | loop | module | logical-operator | mutable | variable | conditional-statement ...