monad_std.result.Result
Bases: Generic[KT, KE]
An ancestor class of any Result
type, inherited by Ok
and Err
subclasses.
ERR: t.Literal[False] = False
class-attribute
instance-attribute
The flag for error value.
See Result.to_pattern
for more information.
OK: t.Literal[True] = True
class-attribute
instance-attribute
The flag for ok value.
See Result.to_pattern
for more information.
__add__(other)
Alias self.__value.__add__
.
Returns:
Type | Description |
---|---|
Result[Any, Any]
|
If both value are |
__bool__()
Returns True
if the result is Ok
.
__mul__(other)
Alias self.__value.__mul__
.
Returns:
Type | Description |
---|---|
Result[Any, Any]
|
If both value are |
and_then(op)
abstractmethod
Calls op
if the result is Ok
, otherwise returns the Err
value of self
.
This function can be used for control flow based on Result
values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
op |
Callable[[KT], Result[U, KE]]
|
The callable object to execute. |
required |
Examples:
bool_and(res)
abstractmethod
Returns res
if the result is Ok
, otherwise returns the Err
value of self
. Alias &(__and__)
.
and
is a keyword of Python, so we use bool_and
instead.
Arguments passed to and are eagerly evaluated.
If you are passing the result of a function call,
it is recommended to use and_then
, which is lazily evaluated.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
res |
Result[U, KE]
|
Another |
required |
Examples:
bool_or(res)
abstractmethod
Returns res
if the result is Err
, otherwise returns the Ok
value of self
. Alias ||(__or__)
.
or
is a keyword of Python, so we use bool_or
instead.
Arguments passed to or
are eagerly evaluated.
If you are passing the result of a function call,
it is recommended to use or_else
, which is lazily evaluated.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
res |
Result[KT, F]
|
Another |
required |
Examples:
catch(func)
staticmethod
Catch a thrown exception from the function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Callable[[], T]
|
A function to catch the thrown exception. |
required |
Returns:
Type | Description |
---|---|
Result[T, Exception]
|
A |
Examples:
catch_from(func, *args, **kwargs)
staticmethod
Catch a thrown exception from a function call.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Callable
|
The function to call |
required |
*args |
Any
|
The arguments passing to |
()
|
**kwargs |
Any
|
The keyword arguments passing to |
{}
|
Returns:
Type | Description |
---|---|
Result
|
A |
Examples:
err()
abstractmethod
Converts from Result<KT, KE>
to Option<KE>
.
Converts self
into an Option<KE>
, and discarding the success value, if any.
Examples:
expect(msg)
abstractmethod
Returns the contained Ok
value.
Because this function may raise an exception, its use is generally discouraged.
Instead, prefer to call unwrap_or
or unwrap_or_else
.
Recommended Message Style
We recommend that expect
messages are used to describe the reason you expect the Result
should be Ok
.
Hint:If you’re having trouble remembering how to phrase expect error messages remember
to focus on the word “should” as in “env variable should be set by blah”
or “the given binary should be available and executable by the current user”.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
msg |
str
|
The message of the exception. |
required |
Raises:
Type | Description |
---|---|
UnwrapException
|
Raises if the value is an |
Examples:
expect_err(msg)
abstractmethod
Returns the contained Err
value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
msg |
str
|
The message of the exception. |
required |
Raises:
Type | Description |
---|---|
UnwrapException
|
Raises if the value is an |
Examples:
flatten()
Converts from Result<Result<T, E>, E>
to Result<T, E>
.
Examples:
inspect(func)
abstractmethod
Calls the provided closure with a reference to the contained value (if Ok
).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Callable[[KT], None]
|
The closure to be executed. |
required |
Examples:
inspect_err(func)
abstractmethod
Calls the provided closure with a reference to the contained value (if Err
).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Callable[[KE], None]
|
The closure to be executed. |
required |
Examples:
is_err()
abstractmethod
is_err_and(func)
abstractmethod
Returns True
if the result is Err
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Callable[[KE], bool]
|
The predicate function. |
required |
Examples:
is_ok()
abstractmethod
is_ok_and(func)
abstractmethod
Returns True
if the result is Ok
and the value inside it matches a predicate.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Callable[[KT], bool]
|
The predicate function. |
required |
Examples:
map(func)
abstractmethod
Maps a Result<KT, KE>
to Result<U, KE>
by applying a function to a contained Ok
value,
leaving an Err
value untouched.
This function can be used to compose the results of two functions.
Examples:
map_err(func)
abstractmethod
Maps a Result<KT, KE>
to Result<KT, F>
by applying a function to a contained Err
value,
leaving the Ok
value untouched.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Callable[[KE], F]
|
The function to apply to the |
required |
Examples:
map_err_mut(func)
abstractmethod
Maps a Result<KT, KE>
's err value by changing it within the closure.
This method require a function to return nothing, and passes a reference into it. Actually, python doesn't differentiate mutable / immutable value, and this method is just for explicit notification.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Callable[[KE], None]
|
A callable object that accepts the wrapped value. |
required |
Examples:
map_mut(func)
abstractmethod
Maps a Result<KT, KE>
's ok value by changing it within the closure.
This method require a function to return nothing, and passes a reference into it. Actually, python doesn't differentiate mutable / immutable value, and this method is just for explicit notification.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Callable[[KT], None]
|
A callable object that accepts the wrapped value. |
required |
Examples:
map_or(default, func)
abstractmethod
Returns the provided default
(if Err
), or applies a function to the contained value (if Ok
).
Arguments passed to map_or
are eagerly evaluated.
If you are passing the result of a function call,
it is recommended to use map_or_else
, which is lazily evaluated.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
default |
U
|
The default value. |
required |
func |
Callable[[KT], U]
|
The function to apply to the result. |
required |
Examples:
map_or_else(default, func)
abstractmethod
Maps a Result<KT, KE>
to U
by applying fallback function default
to a contained Err
value,
or function func
to a contained Ok
value.
This function can be used to unpack a successful result while handling an error.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
default |
Callable[[KE], U]
|
The fallback function to produce a default value. |
required |
func |
Callable[[KT], U]
|
The function to apply to the success value. |
required |
Examples:
of_err(value)
staticmethod
Create an Err
value.
of_ok(value)
staticmethod
Create an Ok
value.
ok()
abstractmethod
Converts from Result<KT, KE>
to Option<KT>
.
Converts self
into an Option<KT>
, and discarding the error, if any.
Examples:
or_else(op)
abstractmethod
Calls op
if the result is Err
, otherwise returns the Ok
value of self.
This function can be used for control flow based on result values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
op |
Callable[[KE], Result[KT, F]]
|
The function to be executed. |
required |
Examples:
to_array()
abstractmethod
to_either()
abstractmethod
to_iter()
Alias iter(self.to_array())
.
to_pattern()
abstractmethod
Returns a flag and the contained value for pattern-matching.
For flags, see Result.OK
and Result.ERR
.
Returns:
Type | Description |
---|---|
Tuple[bool, Union[KT, KE]]
|
A tuple, where the first element is the flag, and the second element is the value. |
Examples:
Built-in pattern-matching is only available in Python 3.10+.
transpose()
unwrap()
abstractmethod
Returns the contained Ok
value.
Because this function may raise an exception, its use is generally discouraged.
Instead, prefer to call unwrap_or
or unwrap_or_else
.
Raises:
Type | Description |
---|---|
UnwrapException
|
Raises if the value is an |
Examples:
unwrap_err()
abstractmethod
Returns the contained Err
value.
Raises:
Type | Description |
---|---|
UnwrapException
|
Raises if the value is an |
Examples:
unwrap_or(default)
abstractmethod
Returns the contained Ok
value or a provided default
.
Arguments passed to unwrap_or are eagerly evaluated.
If you are passing the result of a function call,
it is recommended to use unwrap_or_else
, which is lazily evaluated.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
default |
KT
|
The default value. |
required |
Examples:
unwrap_or_else(op)
abstractmethod
unwrap_unchecked()
abstractmethod
Returns the contained value, no matter what it is.
Examples: