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