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