Level 9 : What will happen when executed?


class RepeatString:
    def __init__(self, string, times):
        self.string = string
        self.times = times
        self.count = 0

    def __iter__(self):
        return self

    def __next__(self):
        if self.count >= self.times:
            raise StopIteration
        self.count += 1
        return self.string * self.count

it = RepeatString("Hi", 3)
print(list(it))
print(list(it))

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