Why only one warning in a loop?

It is by design. See the docs at https://docs.python.org/3/library/warnings.html:

Repetitions of a particular warning for the same source location are typically suppressed.

You can override this behavior by adding a filter with the keyword "always", as in:

import warnings

warnings.simplefilter('always', UserWarning)
for i in range(10):
    warnings.warn('this is a warning message')

Leave a Comment