Skip to content

Instantly share code, notes, and snippets.

@sonkm3
Last active December 16, 2015 20:39
Show Gist options
  • Save sonkm3/5493822 to your computer and use it in GitHub Desktop.
Save sonkm3/5493822 to your computer and use it in GitHub Desktop.
python: ジェネレーター関数の内側でraiseすると、その後はStopIterationしか上がってこない件 ジェネレーター関数内でのraiseはジェネレーター関数内でのreturnと同じ挙動ってこと?
def gen1():
count = 0
while True:
count = count + 1
print count
if count > 10:
return
if count == 5:
print 'raise!'
raise '5!!!'
yield count
counter = gen1()
times = 0
while True:
times = times + 1
if times > 10:
break
try:
count = counter.next()
print count
except TypeError:
print 'except'
exit
continue
#python yield-test.py
#1
#1
#2
#2
#3
#3
#4
#4
#5
#raise!
#except
#Traceback (most recent call last):
# File "yield-test.py", line 24, in <module>
# count = counter.next()
#StopIteration
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment