rlms
rlms

Reputation: 11060

Python generator function premature end exception

What exception should a Python generator function raise when it ends prematurely?

Context: searches of trees represented as classes with __iter__ defined allowing code like for i in BreadthFirstSearch(mytree).

These searches have a max_depth value after which the it should stop returning values.

What exception should be raised when this occurs, or should this be done some other way?

Upvotes: 0

Views: 216

Answers (1)

kindall
kindall

Reputation: 184151

StopIteration is the proper exception to raise to stop an iteration entirely. However, max_depth shouldn't stop the traversal, the traversal should simply not recursively descend into child nodes when it's already at max_depth depth.

Upvotes: 3

Related Questions