Level 6 : What will happen?


class CountDown:
    def __init__(self, start):
        self.current = start

    def __iter__(self):
        return self

    def __next__(self):
        if self.current < 0:
            raise StopIteration
        val = self.current
        self.current -= 2
        return val

cd = CountDown(5)
print(list(cd))
print(list(cd))

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