Level 5 : What happens when the following code is executed?


class CustomIterator:
    def __init__(self, values):
        self.values = values
        self.index = len(values)

    def __iter__(self):
        return self

    def __next__(self):
        if self.index == 0:
            raise StopIteration
        self.index -= 1
        return self.values[self.index]

it = CustomIterator(["A", "B", "C"])
print(list(it))
print(list(it))

Author: SandhyaR | Tags: iterator | 1736+ Python FAQs | Share
sum-function | encapsulation | matplotlib | implementation | loop-control | class | condition | string-formatting | bitwise-operator | mysql | init | asyncio-module | lambda | shallow-copy | python-repl | fabs-function | function-and-scope | dictionaries | indentation | conditional-statement | function | type_conversion | stack | memory | typeerror | magic-method | id-function | type-conversion | logical-operator | while-loop | descriptors | comprehension | dictionary | design-principle | mathematical-function | mutable | debugging | def | __all__-attribute | syntaxerror | generator | functional-programming | split | list-vs-tuple | conversion | datatype | pandas | docstring | sys | input-function | string-concatenation | application | package-management | list-operation | turtle | name-mangling | classes | static-method | syntax-error | memory-management | floating-point-precision | feature | error | installation | map-function | statement | integer | print | math-copysign | keyword | while-and-for-loop | destructor | sorted | deep-copy | exponentiation | built-in-function | head-and-tail | operations | object-attribute | odd-number | for | enumerate() | tuple-unpacking | collection | data_structure | string-manipulation | function-argument | decorator | zip | file-handling | pep | encoding | exception-handling | datetime | class-and-inheritance | operator-overloading | version-check | append | file | while-else-loop | binary-operation | set | library | call | list-comprehension | import-method | global-keyword | virtual-environment | __init_subclass__ | python_version | lambda-function | logical-operation | round-function | escape-operator | generators | bitwise | matrix | else-statement | max-min | module | frozenset | generators-in-python | polymorphism | input-function | regular-expression | benefit | slicing | format | asyncio | goto-function | staticmethod | lambdas | mapping-function | precedence-and-associativity | sum() | immutability | collections | error-handling | nested-loop | built-in-function | software-design | complex | numeric | python-history | recursion | pickling-&-unpickling | built-in | print-statement | syntax | center | class-and-object | variable | exception-handling | tuple | data-structures | oop | slice | counter | loop | file_handling | decimal | objects | parameter | namespace | with-statement | method | choice | control-flow | list-mutation | operator | boolean-and-non-boolean | thread | int | python-module | indexing | global | expand-tab | list-indexing | type-conversion | python-django | expression | binary | filter-function | context-manager | assignment-operator | heap-memory | update | counter-function | iterator | boolean | file-handling | raise | interpreter | methods | identity-operator | string | touples | string-formatting | immutable-data-type | identity-operator | swapping | random | access-modifier | list-slicing | match | operator-or | namedtuple | arithmetic-operation | zip-function | global-variable | min-function | oops | iterable | join | logical-operator | attribute | invalid-list | rollback | scientific-notation | arithmetic-expression | bitwise-operator | set-operation | data-structure | return-statement | higher-order-function | data-type | matrix-list-comprehension | regex | class-and-instance-variable | precedence | conditional-statement | zfill-function | complex-number | formal-and-actual-parameter | min-max-function | lists | comparison | version | functions | gil | factorial-function | mathematics | list | compute | command-line-parsing | yield | round | random-function | comment | dicts | postfix | data-type | syntax-error | numpy | virtual-environment | python | string-slicing | identifier | string-method | list-join | copy | pseudocode | list-method | operators | operator-and | exception | sorting | basic | recurssion | inherit | range | global-variable | loops | operand | assert | array | control-flow | constructor | math | string-iteration | multithreading | __future__ | data-structure | multiprocessing | multiple-inheritance | threading | input | string-method | filter | string-slicing | sys-module | default-mutable-argument | list-comprehension | control-statement | overflow | async | inheritance ...