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