Scala like Option type in Python.
Implements the child classes None and Some, but renamed to none and
some, since None is a built-in type in Python.
This library requires Python 3.12 or newer, because it uses the latest syntax for generics, introduced in Python 3.12.
You can check your python version by running the command:
Windows:
py --versionLinux and macOS:
python3 --version
You can download Python from: Download Python
You can install the package by running the command:
pip install scala-optionFirst, clone the repository or download source code from the latest release.
Next, install the build package:
pip install buildThen, build the project by running:
Windows:
py -m buildLinux and macOS:
python -m build
To install the package locally, run:
pip install -e .Once you have installed the package (see Installation), you just need to add the import statement:
Attention: the package name contains an underscore instead of a dash
from scala_option import Option, none, someThen you can begin using the Option type. Here's a little example:
import random
from scala_option import Option, none, some
def fivety_fivety() -> Option[int]:
if random.random() <= 0.5:
return some(1)
else:
return none
def print_option(option: Option) -> None:
if option.is_empty():
print("You got nothing!")
else:
print(f"You got something: {option.get()}")The Option type implements many of the most important methods
of the Scala Option (Scala Option documentation).
The methods are documented using Docstrings.
List of methods:
get()is_empty()non_empty()get_or_else(default)or_else(alternative)map(f)flat_map(f)fold(if_empty, f)filter(p)exists(p)contains(elem)to_list()