class errstate(contextlib.ContextDecorator):
errstate(**kwargs)
Context manager for floating-point error handling.
Using an instance of errstate
as a context manager allows statements in
that context to execute with a known error handling behavior. Upon entering
the context the error handling is set with seterr
and seterrcall
, and
upon exiting it is reset to what it was before.
errstate
is also usable as a function decorator, saving
a level of indentation if an entire function is wrapped.
See contextlib.ContextDecorator
for more information.seterr, geterr, seterrcall, geterrcall
For complete documentation of the types of floating-point exceptions and
treatment options, see seterr
.
>>> olderr = np.seterr(all='ignore') # Set error handling to known state.
>>> np.arange(3) / 0. array([nan, inf, inf]) >>> with np.errstate(divide='warn'): ... np.arange(3) / 0. array([nan, inf, inf])
>>> np.sqrt(-1) nan >>> with np.errstate(invalid='raise'): ... np.sqrt(-1) Traceback (most recent call last): File "<stdin>", line 2, in <module> FloatingPointError: invalid value encountered in sqrt
Outside the context the error handling behavior has not changed:
>>> np.geterr() {'divide': 'ignore', 'over': 'ignore', 'under': 'ignore', 'invalid': 'ignore'}
Method | __enter__ |
Undocumented |
Method | __exit__ |
Undocumented |
Method | __init__ |
Undocumented |
Instance Variable | call |
Undocumented |
Instance Variable | kwargs |
Undocumented |
Instance Variable | oldcall |
Undocumented |
Instance Variable | oldstate |
Undocumented |