Skip to main content

Exact implementation of Java's Optional in Python with 100% test coverage.

Project description

pyoptional

Java-style Optionals for Python.

Installation

Simply install this package with your favourite package manager:

pip install pyoptional

or

poetry install pyoptional

Usage

The Optional class is a generic class that can be used to wrap a value that may or may not be None. Its functionality is roughly equivalent to Java's Optional class. For example:

Optional<String> opt = Optional.of("Hello, world!");
opt.ifPresent(System.out::println);

turns to the following Python code:

opt = Optional[str].of("Hello, world!")
opt.if_present(print)

Of course, type annotations in Python are optional (no pun intended), so you can simply write Optional.of("Hello, world!") if you prefer.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

joptional-0.1.1.tar.gz (2.3 kB view hashes)

Uploaded Source

Built Distribution

joptional-0.1.1-py3-none-any.whl (2.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page