-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
864f8f3
commit ebfc726
Showing
4 changed files
with
110 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
def bar(): | ||
try: | ||
yield 1 | ||
yield 2 | ||
except ZeroDivisionError: | ||
print("received a ZeroDivisionError") | ||
yield | ||
|
||
|
||
if __name__ == '__main__': | ||
g = bar() | ||
print(g.send(None)) | ||
g.throw(ZeroDivisionError) | ||
g.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
def bar(): | ||
yield | ||
return "bar" | ||
|
||
|
||
def foo(): | ||
name = yield from bar() | ||
print(f"{name = }") | ||
return "foo" | ||
|
||
|
||
if __name__ == '__main__': | ||
coroutine = foo() | ||
try: | ||
coroutine.send(None) | ||
coroutine.send(None) | ||
except StopIteration as e: | ||
print(f"{e.value = }") | ||
import dis | ||
|
||
dis.dis(foo) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
def bar(): | ||
yield 1 | ||
return "Hello World" | ||
|
||
|
||
class Demo: | ||
|
||
def __iter__(self): | ||
return bar() | ||
|
||
|
||
def foo(): | ||
name = yield from Demo() | ||
print(f"{name = }") | ||
return "foo" | ||
|
||
|
||
if __name__ == '__main__': | ||
coroutine = foo() | ||
try: | ||
coroutine.send(None) | ||
coroutine.send(None) | ||
except StopIteration as e: | ||
print(f"{e.value = }") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters