A Python module providing the PyOptional
class/type, a Java-like Optional
wrapper for Python values.
user@machine:~$ pip install pyjoptional
from pyoptional.pyoptional import PyOptional
p: PyOptional[int] = PyOptional.of(1)
p1: PyOptional = PyOptional.of_nullable(None)
p2: PyOptional = PyOptional.empty()
PyOptional.of()
corresponds toOptional.of()
.PyOptional.of_nullable()
corresponds toOptional.ofNullable()
.PyOptional.empty()
corresponds toOptional.empty()
.PyOptional.is_empty()
corresponds toOptional.isEmpty()
.PyOptional.is_present()
corresponds toOptional.isPresent()
.PyOptional.filter()
corresponds toOptional.filter()
.PyOptional.get()
corresponds toOptional.get()
.PyOptional.or_else()
corresponds toOptional.orElse()
.PyOptional.or_else_get()
corresponds toOptional.orElseGet()
.PyOptional.or_else_raise()
corresponds toOptional.orElseThrow()
(both with and without a suppliedException
).PyOptional.or_else_throw
is an alias ofOptional.or_else_raise
.PyOptional.or_new_pyoptional()
corresponds toOptional.or()
(becauseor
is a Python keyword).PyOptional.if_present()
corresponds toOptional.ifPresent()
.PyOptional.if_present_or_else()
corresponds toOptional.ifPresentOrElse()
.PyOptional.map()
corresponds toOptional.map()
.PyOptional.flatMap()
corresponds toOptional.flatMap()
.
Additionally, PyOptional.__eq__
, PyOptional.__hash__
, PyOptional.__str__
, and PyOptional.__repr__
override the default Python implementation of such methods, much like the Java Optional.equals()
, Optional.hashCode()
, and Optional.toString()
methods.
Finally, there is no correspondent of the Java Optional.stream()
method, as Python has no native Stream
class.
The native Python Optional[SomeType]
type is just syntactic sugar for SomeType | None
. Therefore, it does not provide any of the API methods of PyOptional
.