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