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