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