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