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