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