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