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