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