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