Reputation: 11060
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
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