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