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