Level 9 : What happens when we run the following code?


class ReverseIterator:
    def __init__(self, data):
        self.data = data
        self.index = len(data)

    def __iter__(self):
        return self

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

rev = ReverseIterator("Python")
print("".join(rev))

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