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