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