Level 10 : 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 | 1736+ Python FAQs | Share
asyncio | type-conversion | iterable | integer | dictionary | assert | parameter | recurssion | scientific-notation | binary | else-statement | expand-tab | stack | goto-function | data-structures | while-else-loop | turtle | condition | conversion | for | rollback | context-manager | init | sorted | feature | file_handling | list-slicing | zip | mathematical-function | mutable | python-django | name-mangling | math-copysign | version-check | function | staticmethod | random-function | python_version | polymorphism | multiprocessing | global-variable | classes | collections | id-function | call | data-structure | virtual-environment | global-keyword | string-iteration | list-indexing | while-loop | multithreading | loop-control | function-argument | control-flow | numeric | exception-handling | choice | threading | yield | zfill-function | boolean-and-non-boolean | join | async | class-and-inheritance | bitwise | formal-and-actual-parameter | mathematics | mysql | syntax-error | syntax | version | list-comprehension | namespace | objects | gil | operator-overloading | filter-function | class | copy | control-flow | library | string-concatenation | identity-operator | complex-number | access-modifier | methods | __all__-attribute | inheritance | operations | encoding | factorial-function | exponentiation | data-type | file-handling | operator-or | class-and-object | data-type | multiple-inheritance | sorting | installation | __future__ | list-join | list-vs-tuple | file | datetime | counter | dictionaries | filter | object-attribute | print | functions | slice | match | string-manipulation | global-variable | precedence-and-associativity | default-mutable-argument | loops | type_conversion | attribute | asyncio-module | lambda-function | slicing | arithmetic-operation | sum() | overflow | escape-operator | logical-operator | string-method | conditional-statement | floating-point-precision | conditional-statement | odd-number | swapping | built-in-function | input | debugging | min-function | loop | list-comprehension | interpreter | application | round | max-min | counter-function | zip-function | virtual-environment | function-and-scope | module | collection | head-and-tail | oop | memory-management | tuple | lists | string-slicing | def | namedtuple | range | command-line-parsing | while-and-for-loop | operators | exception | assignment-operator | raise | docstring | syntax-error | higher-order-function | matplotlib | invalid-list | with-statement | append | control-statement | bitwise-operator | built-in | mapping-function | sys | set-operation | string-slicing | heap-memory | enumerate() | frozenset | error | thread | decimal | variable | lambdas | update | identity-operator | tuple-unpacking | deep-copy | implementation | file-handling | package-management | logical-operation | sys-module | string-method | python-history | list-method | type-conversion | regex | statement | format | comprehension | basic | complex | decorator | bitwise-operator | postfix | descriptors | inherit | comment | min-max-function | regular-expression | constructor | python | oops | syntaxerror | encapsulation | data_structure | data-structure | operator | arithmetic-expression | python-repl | built-in-function | fabs-function | __init_subclass__ | center | destructor | class-and-instance-variable | string-formatting | datatype | pandas | string-formatting | logical-operator | indexing | matrix | software-design | expression | list | functional-programming | shallow-copy | print-statement | error-handling | map-function | precedence | generators | memory | touples | return-statement | sum-function | typeerror | immutability | round-function | array | nested-loop | int | random | split | matrix-list-comprehension | keyword | global | input-function | indentation | immutable-data-type | import-method | recursion | pickling-&-unpickling | magic-method | compute | method | identifier | static-method | operator-and | lambda | list-mutation | benefit | set | exception-handling | pep | math | boolean | numpy | comparison | dicts | iterator | operand | design-principle | binary-operation | list-operation | string | generators-in-python | input-function | python-module | pseudocode | generator ...