Skip to content

Commit

Permalink
[stop-iteration-return] Use the multiple files template
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Sassoulas committed Sep 13, 2023
1 parent 53025fd commit 6eca2c1
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def fruit_generator():
for fruit in ["apple", "banana"]:
yield fruit
raise StopIteration # [stop-iteration-return]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def two_fruits_generator(fruits):
for fruit in fruits:
yield fruit, next(fruits) # [stop-iteration-return]
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
def fruit_generator():
for fruit in ["apple", "banana"]:
yield fruit
raise StopIteration # [stop-iteration-return]


def two_fruits_generator(fruits):
for fruit in fruits:
yield fruit, next(fruits) # [stop-iteration-return]


def two_good_fruits_generator(fruits):
for fruit in fruits:
if not fruit.is_tasty():
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def fruit_generator():
"""The example is simple enough you don't need an explicit return."""
for fruit in ["apple", "banana"]:
yield fruit
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def two_fruits_generator(fruits):
"""Catching the StopIteration."""
for fruit in fruits:
try:
yield fruit, next(fruits)
except StopIteration:
print("Sorry there is only one fruit left.")
yield fruit, None
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
def fruit_generator():
"""The example is simple enough you don't need an explicit return."""
for fruit in ["apple", "banana"]:
yield fruit


def two_fruits_generator(fruits):
"""Catching the StopIteration."""
for fruit in fruits:
try:
yield fruit, next(fruits)
except StopIteration:
print("Sorry there is only one fruit left.")
yield fruit, None


def two_good_fruits_generator(fruits):
"""A return can be used to end the iterator early, but not a StopIteration."""
for fruit in fruits:
Expand Down

0 comments on commit 6eca2c1

Please sign in to comment.