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