Level 9 : What happens when we execute this code?


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

    def __iter__(self):
        return self

    def __next__(self):
        if self.current >= self.end:
            raise StopIteration
        val = self.current
        self.current *= 2
        return val

it = MultiplyByTwo(1, 20)
print(list(it))

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