Sobes.tech
Junior — Middle
116

Milline tulemus saadakse Pythonis generaatorifunktsiooni täitmise lõppedes?

Vastus AI-lt

sobes.tech AI

When the execution of a generator function in Python completes, a StopIteration exception is raised. This signals that the generator has exhausted all its values and can no longer produce new ones.

If you iterate over the generator in a for loop, this exception is handled automatically, and the loop terminates.

Example:

def gen():
    yield 1
    yield 2

g = gen()
print(next(g))  # Prints 1
print(next(g))  # Prints 2
print(next(g))  # Raises `StopIteration`

Therefore, StopIteration is the standard way to indicate the end of a generator.