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