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