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