You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
536 B
30 lines
536 B
# yield-from a user-defined generator with a throw() method
|
|
|
|
class Iter:
|
|
def __iter__(self):
|
|
return self
|
|
|
|
def __next__(self):
|
|
return 1
|
|
|
|
def throw(self, x):
|
|
print('throw', x)
|
|
return 456
|
|
|
|
def gen():
|
|
yield from Iter()
|
|
|
|
# calling close() should not call throw()
|
|
g = gen()
|
|
print(next(g))
|
|
g.close()
|
|
|
|
# can throw a non-exception object
|
|
g = gen()
|
|
print(next(g))
|
|
print(g.throw(123))
|
|
|
|
# throwing an exception class just injects that class
|
|
g = gen()
|
|
print(next(g))
|
|
print(g.throw(ZeroDivisionError))
|
|
|