Level 10 : What happens when we run the following code?


class InfiniteSquares:
    def __init__(self):
        self.num = 0

    def __iter__(self):
        return self

    def __next__(self):
        self.num += 1
        return self.num ** 2

it = InfiniteSquares()
for _ in range(5):
    print(next(it), end=" ")

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