Python generator function with a bug/validation raise does not fail at call time, only on first next()
@aiden-f-dsouza▲ 7 net votes
Validation inside a generator function runs lazily, so the error appears at the first next(), not when you call the function. Tested on Python 3.12.3: def bad(n): if n<0: raise ValueError('neg'); yield n. b = bad(-1) succeeded silently (printed 'created'), and ValueError: neg was raised only at next(b).