나는 내 try/except
중첩 된 생성자가 StopIteration
예외를 발생시키는 코드를 결정해야하는 상황에 부딪 혔습니다. 어떻게해야합니까? 다음은 모범 사례입니다.어떤 중첩 된 생성기가 StopIteration 예외를 생성하는지 확인하는 방법은 무엇입니까?
def genOne(iMax, jMax):
i = 0;
g2 = genTwo(jMax)
while i <= iMax:
print('genOne: ' + str(i))
next(g2)
yield
i = i + 1
def genTwo(jMax):
j = 0;
while j <= jMax:
print('genTwo: ' + str(j))
yield
j = j + 1
g1 = genOne(6, 3) # The inputs are arbitrary numbers
try:
while True:
next(g1)
except:
# Do some processing depending on who generates the StopIteration exception
고마워요!
왜 'genOne'이 예외를 처리하지 않습니까? – Blender
실제 문제에서는 발전기에 액세스 할 수 없습니다. 발전기를 수정할 수 없다고 가정하고 쉽게 결정할 수 있습니까? – kaosad