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